我只想获取网站URL。不是从链接获取的URL。在页面加载时,我需要能够获取网站的完整、当前URL,并将其设置为一个变量,以便根据需要进行处理。


当前回答

对于带有查询字符串的完整URL:

document.location.toString()

对于主机URL:

window.location

其他回答

您可以通过location.href获取当前页面的完整链接要获取当前控制器的链接,请使用:

location.href.substring(0, location.href.lastIndexOf('/'));

使用:window.location.href。

如上所述,更新window.location时,document.URL不会更新。请参阅MDN。

首先检查页面是否完全加载到

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 );
});

你有多种方法可以做到这一点。

1:

location.href;

2:

document.URL;

3:

document.documentURI;

对于带有查询字符串的完整URL:

document.location.toString()

对于主机URL:

window.location