Get current URL with jQuery?

后端 未结 30 2220
你的背包
你的背包 2020-11-22 02:44

I am using jQuery. How do I get the path of the current URL and assign it to a variable?

Example URL:

http://localhost/menuname.de?foo=bar&nu         


        
相关标签:
30条回答
  • 2020-11-22 03:10

    If you need the hash parameters present in the URL, window.location.href may be a better choice.

    window.location.pathname
    => /search
    
    window.location.href 
     => www.website.com/search#race_type=1
    
    0 讨论(0)
  • 2020-11-22 03:10

    I have this to strip out the GET variables.

    var loc = window.location;
    var currentURL = loc.protocol + '//' + loc.host + loc.pathname;
    
    0 讨论(0)
  • 2020-11-22 03:10

    See purl.js. This will really help and can also be used, depending on jQuery. Use it like this:

    $.url().param("yourparam");
    
    0 讨论(0)
  • 2020-11-22 03:11

    You'll want to use JavaScript's built-in window.location object.

    0 讨论(0)
  • 2020-11-22 03:11

    Just add this function in JavaScript, and it will return the absolute path of the current path.

    function getAbsolutePath() {
        var loc = window.location;
        var pathName = loc.pathname.substring(0, loc.pathname.lastIndexOf('/') + 1);
        return loc.href.substring(0, loc.href.length - ((loc.pathname + loc.search + loc.hash).length - pathName.length));
    }
    

    I hope it works for you.

    0 讨论(0)
  • 2020-11-22 03:13

    You can simply get your path using js itself, window.location or location will give you the object of current URL

    console.log("Origin - ",location.origin);
    console.log("Entire URL - ",location.href);
    console.log("Path Beyond URL - ",location.pathname);

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