aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/api/document/write/index.html
blob: 0037884ad1f0977ec8ddaaf267d59e60d17e0717 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
---
title: Document.write()
slug: Web/API/Document/write
tags:
  - API
  - DOM
  - Document
  - Method
  - Reference
  - write
translation_of: Web/API/Document/write
---
<div>{{ApiRef("DOM")}}</div>

<p><strong><code>Document.write()</code></strong> メソッドは、テキストの文字列を {{domxref("document.open()")}} で開かれた文書ストリームに書き込みます。</p>

<div class="note">注: <code>document.write()</code> は文書の<strong>ストリーム</strong>に書き込みを行うため、閉じられた (読み込み済みの) 文書で <code>document.write()</code> を呼び出すと、自動的に <code>document.open()</code> が呼び出され、<a href="/ja/docs/Web/API/document.open#Notes">文書はクリアされます</a></div>

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

<pre class="brush: js notranslate"><var>document</var>.write(<var>markup</var>);
</pre>

<h3 id="Parameters" name="Parameters">引数</h3>

<dl>
 <dt><var>markup</var></dt>
 <dd>文書に書き込まれるテキストを含む文字列です。</dd>
</dl>

<h3 id="Example" name="Example"></h3>

<pre class="brush: html notranslate">&lt;html&gt;

&lt;head&gt;
  &lt;title&gt;書き込み例&lt;/title&gt;

  &lt;script&gt;
    function newContent() {
      document.open();
      document.write("&lt;h1&gt;新しい内容では、古い内容は削除されます!&lt;/h1&gt;");
      document.close();
    }
  &lt;/script&gt;
&lt;/head&gt;

&lt;body onload="newContent();"&gt;
  &lt;p&gt;元の文書コンテンツ。&lt;/p&gt;
&lt;/body&gt;

&lt;/html&gt;
</pre>

<p>{{EmbedLiveSample("Syntax")}}</p>

<h2 id="Notes" name="Notes"></h2>

<p>出力したテキストは文書構造モデルにおいて構文解析されます。上記の例では、 <code>h1</code> 要素は文書中の一つのノードになります。</p>

<p>{{domxref("document.open()")}} を呼び出さずに読み込まれた文書へ書き込みを行うと、自動的に <code>document.open()</code> の呼び出しが実行されます。一度出力し終わったとき、ブラウザーにページの読み込みの終了を伝えるために、 {{domxref("document.close()")}} を呼び出すことが推奨されます。</p>

<p><code>document.write()</code> の呼び出しがインラインの HTML の <code>&lt;script&gt;</code> タグ内で行われた場合、 <code>document.open()</code> は呼び出されません。次に例を示します。</p>

<pre class="brush: html notranslate">&lt;script&gt;
  document.write("&lt;h1&gt;Main title&lt;/h1&gt;")
&lt;/script&gt;
</pre>

<div class="note"><strong></strong>: <code>document.write()</code> および {{domxref("document.writeln")}} は、 <a href="/ja/docs/Archive/Web/Writing_JavaScript_for_HTML">XHTML 文書では動作しません</a> (エラーコンソールに "Operation is not supported" [<code>NS_ERROR_DOM_NOT_SUPPORTED_ERR</code>] エラーが表示されます)。これは、 .xhtml の拡張子を持つローカルファイルを開いたり、何らかの文書が <code>application/xhtml+xml</code>{{Glossary("MIME type", "MIME タイプ")}}で提供されたときに発生します。詳細は <a class="external" href="http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite">W3C XHTML FAQ</a> で確認することができます。</div>

<div class="note"><strong></strong>: <code>document.write</code><a href="/ja/docs/Web/HTML/Element/script#attr-defer">deferred</a> または <a href="/ja/docs/Web/HTML/Element/script#attr-async">asynchronous</a> のスクリプト内では無視され、エラーコンソールに "A call to <code>document.write()</code> from an asynchronously-loaded external script was ignored" などのメッセージが表示されます。</div>

<div class="note"><strong></strong>: Edge のみ、<code>document.write()</code>{{HTMLElement("iframe")}} 内で複数回呼び出すと、 SCRIPT70: Permission denied エラーが発生します。</div>

<div class="note"><strong></strong>: Chrome 55 からは、キャッシュされていない HTTP の場合、<code>document.write()</code> を通じて挿入された <code>&lt;script&gt;</code> 要素は実行されなくなります。これが成立するのに必要な条件の一覧については、<a href="https://developers.google.com/web/updates/2016/08/removing-document-write">document.write() に対する介入</a>をご覧ください。</div>

<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", "#dom-document-write", "document.write(...)")}}</td>
   <td>{{Spec2("HTML WHATWG")}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName("DOM2 HTML", "html.html#ID-75233634", "document.write(...)")}}</td>
   <td>{{Spec2("DOM2 HTML")}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>

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

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

<ul>
 <li>{{domxref("element.innerHTML")}}</li>
 <li>{{domxref("document.createElement()")}}</li>
</ul>