NOTENotes & quizzesFETCHED
JS 获取当前页面 URL
在 WEB 开发过程中,我们经常需要获取当前页面的 URL,本章节我们为大家介绍如何通过 JavaScript 获取当前页面的 URL。 JS 获取当前页面的 URL 可以使用以下代码: window.location.href 它可以获取完整的 URL。 实例 [mycode4 type='js'] window.location.href [/mycode4] 尝试一下 » 我们也可以获取 URL 的某一..
Updated 7/16/2026
Tutorial detail pages default to noindex.
在 WEB 开发过程中,我们经常需要获取当前页面的 URL,本章节我们为大家介绍如何通过 JavaScript 获取当前页面的 URL。
JS 获取当前页面的 URL 可以使用以下代码:
window.location.href
它可以获取完整的 URL。
我们也可以获取 URL 的某一部分:
实例
var url = window.location.href; // 返回完整 URL (https://www.runoob.com/html/html-tutorial.html)
var pathname = window.location.pathname; // 返回路径部分 (/html/html-tutorial.html)
var origin = window.location.origin; // 返回基础 URL (https://www.runoob.com/)
var domain = document.domain; // 返回域名部分 (www.runoob.com)
var pathname = window.location.pathname; // 返回路径部分 (/html/html-tutorial.html)
var origin = window.location.origin; // 返回基础 URL (https://www.runoob.com/)
var domain = document.domain; // 返回域名部分 (www.runoob.com)
尝试一下 »