我正在使用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);
其他回答
我用这个去掉GET变量。
var loc = window.location;
var currentURL = loc.protocol + '//' + loc.host + loc.pathname;
这也将起作用:
var currentURL = window.location.href;
如果有人想要连接URL和哈希标签,请结合两个功能:
var pathname = window.location.pathname + document.location.hash;
在jstl中,我们可以使用pageContext.request.contextPath访问当前的url路径,
url = "${pageContext.request.contextPath}" + "/controller/path"
示例:在页面中http://stackoverflow.com/questions/406192这将给http://stackoverflow.com/controller/path
// get current URL
$(location).attr('href');
var pathname = window.location.pathname;
alert(window.location);