跳到主要内容

JavaScript 程序:获取当前 URL

示例:获取当前页面的 URL

// 程序用于获取 URL

const url1 = window.location.href;
const url2 = document.URL;
console.log(url1);
console.log(url2);

输出

https://www.google.com/
https://www.google.com/

在上面的程序中,使用了 window.location.href 属性和 document.URL 属性来获取当前页面的 URL。

window.location.hrefdocument.URL 属性都会返回当前页面的 URL。