--- title: Document.domain slug: Web/API/Document/domain tags: - API - DOM - Document - 参考 - 同源策略 - 属性 - 跨域 translation_of: Web/API/Document/domain --- <div>{{ApiRef}}</div> <p>{{domxref("Document")}} 接口的 <strong><code>domain</code></strong> 属性获取/设置当前文档的原始域部分,常用于<a href="/en-US/docs/Web/Security/Same-origin_policy">同源策略</a>。</p> <p>如果成功设置此属性,则原始端口的端口部分也将设置为 <code>null</code>.</p> <h2 id="语法">语法</h2> <pre class="syntaxbox">var <var>domainString</var> = <var>document</var>.domain; <var>document</var>.domain = <var>domainString</var>;</pre> <h3 id="值">值</h3> <p>当前文档来源的域部分.</p> <h3 id="异常">异常</h3> <dl> <dt>安全错误</dt> <dd>已尝试在以下情况之一下设置域: <ul> <li>文件在html中的iframe元素里</li> <li>该文件没有参考上下文</li> <li>该文档的有效域为null</li> <li>给定值不等于文档的有效域(或者它不是该域的可注册域后缀)</li> <li>The {{httpheader('Feature-Policy/document-domain','document-domain')}} {{HTTPHeader("Feature-Policy")}}一启用</li> </ul> </dd> </dl> <h2 id="例子">例子</h2> <h3 id="获取域名">获取域名</h3> <p>对于URI http://developer.mozilla.org/en-US/docs/Web,此示例将currentDomain设置为字符串“ developer.mozilla.org”。</p> <pre class="brush:js">var currentDomain = document.domain;</pre> <h3 id="关闭窗口">关闭窗口</h3> <p>如果文档(例如www.example.xxx/good.html)的域为“ www.example.xxx”,则本示例将尝试关闭窗口。</p> <pre class="brush:js">var badDomain = "www.example.xxx"; if (document.domain == badDomain) { // 这只是一个示例 - 有时 window.close() 是没有效果的 window.close(); } </pre> <h2 id="规范">规范</h2> <table class="standard-table"> <thead> <tr> <th scope="col">规范</th> <th scope="col">状态</th> <th scope="col">备注</th> </tr> </thead> <tbody> <tr> <td>{{SpecName('HTML WHATWG','origin.html#relaxing-the-same-origin-restriction','Document.domain')}}</td> <td>{{Spec2('HTML WHATWG')}}</td> <td></td> </tr> </tbody> </table> <h2 id="浏览器兼容性">浏览器兼容性</h2> <div>{{Compat("api.Document.domain")}}</div> <h3 id="Firefox_备注">Firefox 备注</h3> <p>如果当前文档的域无法识别,那么 domain 属性会返回 null。但这个表现在 Firefox 62 时发生了更改——详见 {{bug(819475)}} 中的讨论。</p> <p>在<a href="/en-US/docs/XPCOM_Interface_Reference/nsIEffectiveTLDService#getBaseDomain.28.29">根域名</a>范围内,Mozilla 允许你把 domain 属性的值设置为它的上一级域。例如,在 developer.mozilla.org 域内,可以把 domain 设置为 "mozilla.org" 但不能设置为 "mozilla.com" 或者"org"。</p> <p>Mozilla 会区分 <code>document.domain</code> 属性 <strong>从没有被设定过值 </strong>和 <strong>被显示的设定为跟该文档的 URL 的 domain 一致的值</strong>,尽管这两种状况下,该属性会返回同样的值。两个文档,只有在 <code>document.domain</code> 都被设定为同一个值,表明他们打算协作;或者都没有设定 <code>document.domain</code> 属性并且URL的域是一致的 (<a href="https://mxr.mozilla.org/mozilla-central/source/caps/src/nsScriptSecurityManager.cpp#1003">如何判断一致</a>),这两种条件下,一个文档才可以去访问另一个文档。如果没有这个特殊的策略,每一个站点都会成为他的子域的 XSS 攻击的对象(例如,<a class="link-https" href="https://bugzilla.mozilla.org" rel="freelink">https://bugzilla.mozilla.org</a> 可以被来自 <a class="link-https" href="https://bug*.bugzilla.mozilla.org" rel="freelink">https://bug*.bugzilla.mozilla.org</a> 站点的 bug 附件攻击)。</p> <h2 id="参见">参见</h2> <ul> <li><a href="/en-US/docs/Web/Security/Same-origin_policy">Same origin policy for JavaScript</a></li> </ul>