大网数据广告活动
Web 标准与前端开发

Document.location

· 52CE 我爱测

**`Document.location`** 是一个只读属性,返回一个 Location 对象,包含有文档的 URL 相关的信息,并提供了改变该 URL 和加载其他 URL 的方法。

Document.location 是一个只读属性,返回一个 Location 对象,包含有文档的 URL 相关的信息,并提供了改变该 URL 和加载其他 URL 的方法。

尽管 Document.location 是一个只读的 Location 对象,你也能够赋给它一个 String。这意味着你能够赋给 document.location 字符串,大多数情况下像这样使用:document.location = 'http://www.example.com',也可写为document.location.href = 'http://www.example.com'。

只是想获取字符串形式的 URL,可以使用只读属性 document.URL。

If the current document is not in a browsing context, the returned value is null.

语法

locationObj = document.location;
document.location = "http://www.mozilla.org"; // Equivalent to document.location.href = 'http://www.mozilla.org'

示例

dump(document.location);
// Prints a string like
// "http://www.example.com/juicybits.html" to the console

规范

浏览器兼容性

参见

  • The interface of the returned value, Location.
  • A similar information, but attached to the browsing context, Window.location

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。