我只想获取网站URL。不是从链接获取的URL。在页面加载时,我需要能够获取网站的完整、当前URL,并将其设置为一个变量,以便根据需要进行处理。
当前回答
你有多种方法可以做到这一点。
1:
location.href;
2:
document.URL;
3:
document.documentURI;
其他回答
获取当前页面URL:
window.location.href
在jstl中,我们可以使用pageContext.request.contextPath访问当前的URL路径。如果要进行Ajax调用,请使用以下URL。
url = "${pageContext.request.contextPath}" + "/controller/path"
示例:对于页面http://stackoverflow.com/posts/36577223这将给http://stackoverflow.com/controller/path.
你有多种方法可以做到这一点。
1:
location.href;
2:
document.URL;
3:
document.documentURI;
使用:window.location.href。
如上所述,更新window.location时,document.URL不会更新。请参阅MDN。
var currentPageUrlIs = "";
if (typeof this.href != "undefined") {
currentPageUrlIs = this.href.toString().toLowerCase();
}else{
currentPageUrlIs = document.location.toString().toLowerCase();
}
以上代码也可以帮助某人