Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1030334 of Document.write()

  • Revision slug: Web/API/Document/write
  • Revision title: Document.write()
  • Revision id: 1030334
  • Created:
  • Creator: Tigt
  • Is current revision? No
  • Comment

Revision Content

{{ ApiRef("DOM") }}

Writes a string of text to a document stream opened by document.open().

Note: as document.write writes to the document stream, calling document.write on a closed (loaded) document automatically calls document.open, which will clear the document.

Syntax

document.write(markup);

markup is a string containing the text to be written to the document.

Example

<html>

<head>
<title>write example</title>

<script>

function newContent()
{
alert("load new content");
document.open();
document.write("<h1>Out with the old - in with the new!</h1>");
document.close();
}

</script>
</head>

<body onload="newContent();">
<p>Some original document content.</p>
</body>
</html>

Notes

Writing to a document that has already loaded without calling document.open() will automatically perform a document.open call. Once you have finished writing, it is recommended to call document.close(), to tell the browser to finish loading the page. The text you write is parsed into the document's structure model. In the example above, the h1 element becomes a node in the document.

If the document.write() call is embedded within an inlined HTML <script> tag, then it will not call document.open(). For example:

<script> 
  document.write("<h1>Main title</h1>") 
</script> 
Note: document.write and document.writeln do not work in XHTML documents (you'll get a "Operation is not supported" [NS_ERROR_DOM_NOT_SUPPORTED_ERR] error in the error console). This happens when opening a local file with the .xhtml file extension or for any document served with an application/xhtml+xml MIME type. More information is available in the W3C XHTML FAQ.
Note: document.write in deferred or asynchronous scripts will be ignored, and you'll get a message like "A call to document.write() from an asynchronously-loaded external script was ignored" in the error console.

Specifications

See also

  • {{ domxref("element.innerHTML") }}
  • {{ domxref("document.createElement()") }}

Revision Source

<div>{{ ApiRef("DOM") }}</div>

<p>Writes a string of text to a document stream opened by <a href="/en-US/docs/Web/API/document.open">document.open()</a>.</p>

<div class="note">Note: as <code>document.write</code> writes to the document <strong>stream</strong>, calling <code>document.write</code> on a closed (loaded) document automatically calls <code>document.open</code>, <a href="https://developer.mozilla.org/en-US/docs/Web/API/document.open#Notes">which will clear the document</a>.</div>

<h2 id="Syntax">Syntax</h2>

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

<p><code>markup</code> is a string containing the text to be written to the document.</p>

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

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

&lt;head&gt;
&lt;title&gt;write example&lt;/title&gt;

&lt;script&gt;

function newContent()
{
alert("load new content");
document.open();
document.write("&lt;h1&gt;Out with the old - in with the new!&lt;/h1&gt;");
document.close();
}

&lt;/script&gt;
&lt;/head&gt;

&lt;body onload="newContent();"&gt;
&lt;p&gt;Some original document content.&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>

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

<p>Writing to a document that has already loaded without calling <a href="/en-US/docs/Web/API/document.open"><code>document.open()</code></a> will automatically perform a <code>document.open</code> call. Once you have finished writing, it is recommended to call <a href="/en-US/docs/Web/API/document.close"><code>document.close()</code></a>, to tell the browser to finish loading the page. The text you write is parsed into the document's structure model. In the example above, the <code>h1</code> element becomes a node in the document.</p>

<p>If the <code>document.write()</code> call is embedded within an inlined HTML <code>&lt;script&gt;</code> tag, then it will not call <code>document.open()</code>. For example:</p>

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

<div class="note"><strong>Note:</strong> <code>document.write</code> and <code>document.writeln</code> <a href="/en-US/docs/Archive/Web/Writing_JavaScript_for_HTML">do not work in XHTML documents</a> (you'll get a "Operation is not supported" [<code>NS_ERROR_DOM_NOT_SUPPORTED_ERR</code>] error in the error console). This happens when opening a local file with the .xhtml file extension or for any document served with an <code>application/xhtml+xml</code> <a href="/en-US/docs/Glossary/MIME_type">MIME type</a>. More information is available in the <a class="external" href="https://www.w3.org/MarkUp/2004/xhtml-faq#docwrite">W3C XHTML FAQ</a>.</div>

<div class="note"><strong>Note:</strong> <code>document.write</code> in <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-defer">deferred</a> or <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-async">asynchronous</a> scripts will be ignored, and you'll get a message like "A call to <code>document.write()</code> from an asynchronously-loaded external script was ignored" in the error console.</div>

<h2 id="Specification">Specifications</h2>

<ul>
 <li><a class="external" href="https://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-75233634">DOM Level 2 HTML: <code>write()</code> Method</a></li>
 <li><a class="external" href="https://www.w3.org/TR/2011/WD-html5-author-20110705/apis-in-html-documents.html#dynamic-markup-insertion">Dynamic markup insertion in HTML</a></li>
</ul>

<h2 id="See_also">See also</h2>

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