---
title: Document.domain
slug: Web/API/Document/domain
tags:
  - API
  - Document
  - HTML DOM
  - Property
  - Reference
translation_of: Web/API/Document/domain
---
<div>{{ApiRef}}</div>

<p>{{domxref("Document")}} インターフェイスの <strong><code>domain</code></strong> プロパティは、<a href="/ja/docs/Web/Security/Same-origin_policy">同一オリジンポリシー</a>で使用される現在の文書の{{glossary("origin", "オリジン")}}のうち、ドメインの部分を取得または設定します。</p>

<p>このプロパティが正常に設定されると、オリジンのポート番号の部分も {{jsxref("null")}} に設定されます。</p>

<h2 id="Syntax" name="Syntax">構文</h2>

<pre class="syntaxbox notranslate">const <var>domainString</var> = <var>document</var>.domain
<var>document</var>.domain = <var>domainString</var></pre>

<h3 id="Value" name="Value">値</h3>

<p>現在の文書のオリジンのうち、ドメインの部分です。</p>

<h3 id="Exceptions" name="Exceptions">例外</h3>

<dl>
 <dt><code>SecurityError</code></dt>
 <dd>以下の状況のうちの一つで、 <code>domain</code> を設定することが試みられた。
 <ul>
  <li>文書がサンドボックス化された {{htmlelement("iframe")}} の中にある場合</li>
  <li>文書に{{glossary("browsing context", "閲覧コンテキスト")}}がない場合</li>
  <li>文書の<a href="https://html.spec.whatwg.org/multipage/origin.html#concept-origin-effective-domain">影響ドメイン</a>が <code>null</code> の場合</li>
  <li>指定された値が文書の影響ドメインと一致しない (または登録可能なドメインの接頭辞ではない) 場合</li>
  <li>{{HTTPHeader("Feature-Policy")}} の {{httpheader('Feature-Policy/document-domain','document-domain')}} が有効の場合</li>
 </ul>
 </dd>
</dl>

<h2 id="Examples" name="Examples">例</h2>

<h3 id="Getting_the_domain" name="Getting_the_domain">ドメイン名の取得</h3>

<p><code>http://developer.mozilla.org/en-US/docs/Web</code> の URI において、この例は <code>currentDomain</code> に "<code>developer.mozilla.org</code>" の文字列を設定します。</p>

<pre class="brush:js notranslate">const currentDomain = document.domain;</pre>

<h3 id="Closing_a_window" name="Closing_a_window">ウィンドウを閉じる</h3>

<p>文書が <code>www.example.xxx/good.html</code> のような "<code>www.example.xxx</code>" のドメインを持つ場合、この例はウィンドウを閉じようとします。</p>

<pre class="brush:js notranslate">const badDomain = "www.example.xxx";

if (document.domain === badDomain) {
  // 単なる例: window.close() は効果がないことがある
  window.close();
}
</pre>

<h2 id="Specifications" name="Specifications">仕様書</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="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>

<div>{{Compat("api.Document.domain")}}</div>

<h2 id="See_also" name="See_also">関連情報</h2>

<ul>
 <li><a href="/ja/docs/Web/Security/Same-origin_policy">同一オリジンポリシー</a></li>
</ul>