我正在使用jQuery。如何获取当前URL的路径并将其分配给变量?
示例URL:
http://localhost/menuname.de?foo=bar&number=0
我正在使用jQuery。如何获取当前URL的路径并将其分配给变量?
示例URL:
http://localhost/menuname.de?foo=bar&number=0
当前回答
// get current URL
$(location).attr('href');
var pathname = window.location.pathname;
alert(window.location);
其他回答
在jstl中,我们可以使用pageContext.request.contextPath访问当前的url路径,
url = "${pageContext.request.contextPath}" + "/controller/path"
示例:在页面中http://stackoverflow.com/questions/406192这将给http://stackoverflow.com/controller/path
如果有人想要连接URL和哈希标签,请结合两个功能:
var pathname = window.location.pathname + document.location.hash;
在纯jQuery样式中:
$(location).attr('href');
location对象还具有其他财产,如host、hash、protocol和pathname。
最短的方法(11个字符)是
让myUrl=“”+位置console.log(myUrl);
这也将起作用:
var currentURL = window.location.href;