我只想获取网站URL。不是从链接获取的URL。在页面加载时,我需要能够获取网站的完整、当前URL,并将其设置为一个变量,以便根据需要进行处理。
当前回答
对于那些想要一个实际的URL对象的人,可能是一个以URL为参数的实用程序:
const url = new URL(window.location.href)
https://developer.mozilla.org/en-US/docs/Web/API/URL
其他回答
使用此项:
var url=window.location.href;console.log(url);
使用:window.location.href。
如上所述,更新window.location时,document.URL不会更新。请参阅MDN。
对于那些想要一个实际的URL对象的人,可能是一个以URL为参数的实用程序:
const url = new URL(window.location.href)
https://developer.mozilla.org/en-US/docs/Web/API/URL
使用window.location对与当前帧关联的位置对象进行读写访问。如果您只想以只读字符串形式获取地址,可以使用document.URL,它应该包含与window.location.href相同的值。
location.origin+location.pathname+location.search+location.hash;
and
location.href
也一样。