此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

Document.domain

Deprecated

Avoid using this feature in new projects. This feature may be a candidate for removal from web standards or browsers.

Document 接口的 domain 属性获取/设置当前文档来源的域名部分,常用于同源策略

当前文档来源的域部分。

异常

SecurityError DOMException

已尝试在以下情况之一下设置域:

  • 文件在 html 中的 iframe 元素里
  • 该文件没有参考上下文
  • 该文档的有效域为 null
  • 给定值不等于文档的有效域(或者它不是该域的可注册域后缀)
  • The document-domain Permissions-Policy一启用

示例

获取域名

对于 URI http://developer.mozilla.org/zh-CN/docs/Web,此示例将 currentDomain 设置为字符串 developer.mozilla.org

js
const currentDomain = document.domain;

关闭窗口

如果文档(例如 www.example.xxx/good.html)的域为“www.example.xxx”,则本示例将尝试关闭窗口。

js
var badDomain = "www.example.xxx";

if (document.domain == badDomain) {
  // 这只是一个示例 - 有时 window.close() 是没有效果的
  window.close();
}

规范

规范
HTML
# relaxing-the-same-origin-restriction

浏览器兼容性

参见