JavaScript 程序:获取当前URL

示例:获取当前 URL

// program to get the 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。

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战