我只想获取网站URL。不是从链接获取的URL。在页面加载时,我需要能够获取网站的完整、当前URL,并将其设置为一个变量,以便根据需要进行处理。
当前回答
使用window.location.href获取完整的URL。使用window.location.pathname获取离开主机的URL。
其他回答
获取当前页面URL:
window.location.href
您可以使用以下方法获取带有哈希标记的当前URL位置:
JavaScript:
// Using href
var URL = window.location.href;
// Using path
var URL = window.location.pathname;
jQuery:
$(location).attr('href');
使用JavaScript获取当前URL:
window.location.toString();窗口.location.href
您可以通过location.href获取当前页面的完整链接要获取当前控制器的链接,请使用:
location.href.substring(0, location.href.lastIndexOf('/'));
Use:
window.location.href
正如评论中所指出的,下面这行行是有效的,但它对Firefox来说是错误的。
document.URL
请参阅DOMString类型的URL,只读。