我只想获取网站URL。不是从链接获取的URL。在页面加载时,我需要能够获取网站的完整、当前URL,并将其设置为一个变量,以便根据需要进行处理。
当前回答
使用JavaScript获取当前URL:
window.location.toString();窗口.location.href
其他回答
打开开发人员工具,在控制台中键入以下内容,然后按Enter键。
window.location
下面是当前页面上的结果截图。
从这里获取您需要的信息。:)
您可以使用以下方法获取带有哈希标记的当前URL位置:
JavaScript:
// Using href
var URL = window.location.href;
// Using path
var URL = window.location.pathname;
jQuery:
$(location).attr('href');
location.origin+location.pathname+location.search+location.hash;
and
location.href
也一样。
首先检查页面是否完全加载到
browser,window.location.toString();
window.location.href
然后调用一个获取url、url变量并在控制台上打印的函数,
$(window).load(function(){
var url = window.location.href.toString();
var URL = document.URL;
var wayThreeUsingJQuery = $(location).attr('href');
console.log(url);
console.log(URL);
console.log(wayThreeUsingJQuery );
});
使用:window.location.href。
如上所述,更新window.location时,document.URL不会更新。请参阅MDN。