Is there any method to get the URL without query string?

后端 未结 14 1223
天涯浪人
天涯浪人 2020-11-28 19:22

I have a URL like http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235.

I want to get the URL without the query s

相关标签:
14条回答
  • 2020-11-28 19:34

    Just add these two lines to $(document).ready in JS as follow:

    $(document).ready(function () {
     $("div.sidebar nav a").removeClass("active");
            $('nav a[href$="'+ window.location.pathname.split("?")[0] +'"]').addClass('active');
    });
    

    it is better to use the dollar sign ($) (End with)

    $('nav a[href$
    

    instead of (^) (Start with)

    $('nav a[href^
    

    because, if you use the (^) sign and you have nested URLs in the navigation menu, (e.g "/account" and "/account/roles")

    It will active both of them.

    0 讨论(0)
  • 2020-11-28 19:35

    If you also want to remove hash, try this one: window.location.href.split(/[?#]/)[0]

    0 讨论(0)
  • 2020-11-28 19:39
    var url = window.location.origin + window.location.pathname;
    
    0 讨论(0)
  • 2020-11-28 19:45

    Use properties of window.location

    var loc = window.location;
    var withoutQuery = loc.hostname + loc.pathname;
    var includingProtocol = loc.protocol + "//" + loc.hostname + loc.pathname;
    

    You can see more properties at https://developer.mozilla.org/en/DOM/window.location

    0 讨论(0)
  • 2020-11-28 19:45

    Here are two methods:

    <script type="text/javascript">
        var s="http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu
                                    =true&pcode=1235";
    
        var st=s.substring(0, s.indexOf("?"));
    
        alert(st);
    
        alert(s.replace(/\?.*/,''));
    </script>
    
    0 讨论(0)
  • 2020-11-28 19:46

    Try this: window.location.href.split('?')[0]

    0 讨论(0)
提交回复
热议问题