document.write

  • Revision slug: DOM/document.write
  • Revision title: document.write
  • Revision id: 74057
  • Created:
  • Creator: PGaries
  • Is current revision? No
  • Comment /* Specification */ Updated W3C DOM Level 2 HTML specifcation URI.

Revision Content

{{template.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>

Specification

DOM Level 2 HTML: write() Method

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

Revision Source

<p>
{{template.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">document.open()</a>.
</p>
<h3 name="Syntax"> Syntax </h3>
<pre class="eval">document.write(<i>markup</i>);
</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">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">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>
<h3 name="Specification">Specification </h3>
<p><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>
</p>{{ wiki.languages( { "fr": "fr/DOM/document.write", "pl": "pl/DOM/document.write" } ) }}
Revert to this revision