document.write

  • Revision slug: DOM/document.write
  • Revision title: document.write
  • Revision id: 74064
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment 11 words added, 14 words removed

Revision Content

{{ DomRef() }}

Summary

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

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 type="text/javascript">

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 directly in the HTML code, then it will not call document.open(). For example:

 <div> 
  <script type="text/javascript"> 
    document.write("<h1>Main title</h1>") 
  </script> 
 </div>
Note: document.write (like document.writeln) does not work in XHTML documents (you'll get a "Operation is not supported" (NS_ERROR_DOM_NOT_SUPPORTED_ERR) error on the error console). This is the case if opening a local file with a .xhtml file extension or for any document served with an application/xhtml+xml MIME type. More information is available in the W3C XHTML FAQ.

Specification

See also

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

{{ languages( { "fr": "fr/DOM/document.write", "ja": "ja/DOM/document.write", "pl": "pl/DOM/document.write" } ) }}

Revision Source

<p>{{ DomRef() }}</p>
<h3 name="Summary">Summary</h3>
<p>Writes a string of text to a document stream opened by <a href="/en/DOM/document.open" title="en/DOM/document.open">document.open()</a>.</p>
<h3 name="Syntax">Syntax</h3>
<pre class="eval">document.write(<em>markup</em>);
</pre>
<ul> <li><code>markup</code> is a string containing the text to be written to the document.</li>
</ul>
<h3 name="Example">Example</h3>
<pre>&lt;html&gt;

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

&lt;script type="text/javascript"&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>
<h3 name="Notes">Notes</h3>
<p>Writing to a document that has already loaded without calling <a href="/en/DOM/document.open" title="en/DOM/document.open">document.open()</a> will automatically perform a <code>document.open</code> call. Once you have finished writing, it is recommended to call <a href="/en/DOM/document.close" title="en/DOM/document.close">document.close()</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 directly in the HTML code, then it will not call <code>document.open()</code>. For example:</p>
<pre> &lt;div&gt; 
  &lt;script type="text/javascript"&gt; 
    document.write("&lt;h1&gt;Main title&lt;/h1&gt;") 
  &lt;/script&gt; 
 &lt;/div&gt;
</pre>
<div class="note"><strong>Note:</strong> <strong>document.write</strong> (like <strong>document.writeln</strong>) does not work in XHTML documents (you'll get a "Operation is not supported" (<code>NS_ERROR_DOM_NOT_SUPPORTED_ERR</code>) error on the error console). This is the case if opening a local file with a .xhtml file extension or for any document served with an application/xhtml+xml MIME type. More information is available in the <a class=" external" href="http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite" title="http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite">W3C XHTML FAQ</a>.</div>
<h3 name="Specification">Specification</h3>
<ul> <li><a class="external" href="http://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="http://www.w3.org/html/wg/html5/#dom-document-write-html" title="http://www.w3.org/html/wg/html5/#dom-document-write-html">Dynamic markup insertion in HTML</a></li>
</ul>
<h3 name="See_also">See also</h3>
<ul> <li>{{ domxref("element.innerHTML") }}</li> <li>{{ domxref("document.createElement()") }}</li>
</ul>
<p>{{ languages( { "fr": "fr/DOM/document.write", "ja": "ja/DOM/document.write", "pl": "pl/DOM/document.write" } ) }}</p>
Revert to this revision