From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/document/domain/index.html | 101 +++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 files/zh-cn/web/api/document/domain/index.html (limited to 'files/zh-cn/web/api/document/domain') diff --git a/files/zh-cn/web/api/document/domain/index.html b/files/zh-cn/web/api/document/domain/index.html new file mode 100644 index 0000000000..b6951d94a0 --- /dev/null +++ b/files/zh-cn/web/api/document/domain/index.html @@ -0,0 +1,101 @@ +--- +title: Document.domain +slug: Web/API/Document/domain +tags: + - API + - DOM + - Document + - 参考 + - 同源策略 + - 属性 + - 跨域 +translation_of: Web/API/Document/domain +--- +
{{ApiRef}}
+ +

{{domxref("Document")}} 接口的 domain 属性获取/设置当前文档的原始域部分,常用于同源策略

+ +

如果成功设置此属性,则原始端口的端口部分也将设置为 null.

+ +

语法

+ +
var domainString = document.domain;
+document.domain = domainString;
+ +

+ +

当前文档来源的域部分.

+ +

异常

+ +
+
安全错误
+
已尝试在以下情况之一下设置域: +
    +
  • 文件在html中的iframe元素里
  • +
  • 该文件没有参考上下文
  • +
  • 该文档的有效域为null
  • +
  • 给定值不等于文档的有效域(或者它不是该域的可注册域后缀)
  • +
  • The {{httpheader('Feature-Policy/document-domain','document-domain')}} {{HTTPHeader("Feature-Policy")}}一启用
  • +
+
+
+ +

例子

+ +

获取域名

+ +

对于URI http://developer.mozilla.org/en-US/docs/Web,此示例将currentDomain设置为字符串“ developer.mozilla.org”。

+ +
var currentDomain = document.domain;
+ +

关闭窗口

+ +

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

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

规范

+ + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName('HTML WHATWG','origin.html#relaxing-the-same-origin-restriction','Document.domain')}}{{Spec2('HTML WHATWG')}}
+ +

浏览器兼容性

+ + + +
{{Compat("api.Document.domain")}}
+ +

Firefox 备注

+ +

如果当前文档的域无法识别,那么 domain 属性会返回 null。但这个表现在 Firefox 62 时发生了更改——详见 {{bug(819475)}} 中的讨论。

+ +

根域名范围内,Mozilla 允许你把 domain 属性的值设置为它的上一级域。例如,在 developer.mozilla.org 域内,可以把 domain 设置为 "mozilla.org" 但不能设置为 "mozilla.com" 或者"org"。

+ +

Mozilla 会区分 document.domain 属性 从没有被设定过值 被显示的设定为跟该文档的 URL 的 domain 一致的值,尽管这两种状况下,该属性会返回同样的值。两个文档,只有在 document.domain 都被设定为同一个值,表明他们打算协作;或者都没有设定 document.domain 属性并且URL的域是一致的 (如何判断一致),这两种条件下,一个文档才可以去访问另一个文档。如果没有这个特殊的策略,每一个站点都会成为他的子域的 XSS 攻击的对象(例如,https://bugzilla.mozilla.org 可以被来自 https://bug*.bugzilla.mozilla.org 站点的 bug 附件攻击)。

+ +

参见

+ + -- cgit v1.2.3-54-g00ecf