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/ja/web/api/document/write/index.html | 112 +++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 files/ja/web/api/document/write/index.html (limited to 'files/ja/web/api/document/write/index.html') diff --git a/files/ja/web/api/document/write/index.html b/files/ja/web/api/document/write/index.html new file mode 100644 index 0000000000..001be34b9f --- /dev/null +++ b/files/ja/web/api/document/write/index.html @@ -0,0 +1,112 @@ +--- +title: Document.write() +slug: Web/API/Document/write +tags: + - API + - DOM + - Document + - Method + - Reference + - write +translation_of: Web/API/Document/write +--- +
{{ApiRef("DOM")}}
+ +

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

+ +
注: document.write() は文書のストリームに書き込みを行うため、閉じられた (読み込み済みの) 文書で document.write() を呼び出すと、自動的に document.open() が呼び出され、文書はクリアされます
+ +

構文

+ +
document.write(markup);
+
+ +

引数

+ +
+
markup
+
文書に書き込まれるテキストを含む文字列です。
+
+ +

+ +
<html>
+
+<head>
+  <title>書き込み例</title>
+
+  <script>
+    function newContent() {
+      document.open();
+      document.write("<h1>新しい内容では、古い内容は削除されます!</h1>");
+      document.close();
+    }
+  </script>
+</head>
+
+<body onload="newContent();">
+  <p>元の文書コンテンツ。</p>
+</body>
+
+</html>
+
+ +

{{EmbedLiveSample("Syntax")}}

+ +

+ +

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

+ +

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

+ +

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

+ +
<script>
+  document.write("<h1>Main title</h1>")
+</script>
+
+ +
: document.write() および {{domxref("document.writeln")}} は、 XHTML 文書では動作しません (エラーコンソールに "Operation is not supported" [NS_ERROR_DOM_NOT_SUPPORTED_ERR] エラーが表示されます)。これは、 .xhtml の拡張子を持つローカルファイルを開いたり、何らかの文書が application/xhtml+xml の {{Glossary("MIME type", "MIME タイプ")}}で提供されたときに発生します。詳細は W3C XHTML FAQ で確認することができます。
+ +
: document.writedeferred または asynchronous のスクリプト内では無視され、エラーコンソールに "A call to document.write() from an asynchronously-loaded external script was ignored" などのメッセージが表示されます。
+ +
: Edge のみ、document.write() を {{HTMLElement("iframe")}} 内で複数回呼び出すと、 SCRIPT70: Permission denied エラーが発生します。
+ +
: Chrome 55 からは、キャッシュされていない HTTP の場合、document.write() を通じて挿入された <script> 要素は実行されなくなります。これが成立するのに必要な条件の一覧については、document.write() に対する介入をご覧ください。
+ +

仕様書

+ + + + + + + + + + + + + + + + + + + + + +
仕様書状態備考
{{SpecName("HTML WHATWG", "#dom-document-write", "document.write(...)")}}{{Spec2("HTML WHATWG")}}
{{SpecName("DOM2 HTML", "html.html#ID-75233634", "document.write(...)")}}{{Spec2("DOM2 HTML")}}
+ +

ブラウザーの互換性

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

関連情報

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