Get current URL with jQuery?

后端 未结 30 2643
你的背包
你的背包 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: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.

提交回复
热议问题