document.open

  • Revision slug: DOM/document.open
  • Revision title: document.open
  • Revision id: 79154
  • Created:
  • Creator: dbruant
  • Is current revision? No
  • Comment 6 words removed

Revision Content

Summary

The document.open() method opens a document for writing.

Syntax

document.open();

Example

// In this example, the document contents are 
// overwritten as the document 
// is reinitialized on open(). 
document.write("<html><p>remove me</p></html>"); 
document.open(); 
// document is empty.

Notes

If a document exists in the target, this method clears it (see the example above).

Also, an automatic document.open() call happens when document.write() is called after the page has loaded, but that's not defined in the W3C specification. document non-spec'ed parameters to document.open

Do not confuse this method with window.open(). document.open allows you to overwrite the current document or append to it, while window.open provides a way to open a new window, leaving the current document intact. Since window is the global object, just calling open(...) does the same as window.open(...).

You can close the opened document using document.close().

{{ gecko_minversion_note("1.9", "Starting with Gecko 1.9, this method is subject to the same same-origin policy as other properties, and does not work if doing so would change the document's origin.") }}

{{ gecko_minversion_note("1.9.2", "Starting with Gecko 1.9.2, document.open() uses the principal of the document whose URI it uses, instead of fetching the principal off the stack. As a result, you can no longer call document.write() into an untrusted document from chrome, even using wrappedJSObject.") }}

See Security check basics for more about principals.

Specification

DOM Level 2 HTML: open() Method

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

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code>document.open()</code> method opens a document for <a href="/en/DOM/document.write" title="en/DOM/document.write">writing</a>.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="eval">document.open();
</pre>
<h2 id="Example" name="Example">Example</h2>
<pre>// In this example, the document contents are 
// overwritten as the document 
// is reinitialized on open(). 
document.write("&lt;html&gt;&lt;p&gt;remove me&lt;/p&gt;&lt;/html&gt;"); 
document.open(); 
// document is empty.
</pre>
<h2 id="Notes" name="Notes">Notes</h2>
<p>If a document exists in the target, this method clears it (see the example above).</p>
<p>Also, an automatic <code>document.open()</code> call happens when <a href="/en/DOM/document.write" title="en/DOM/document.write">document.write()</a> is called after the page has loaded, but that's not defined in the W3C specification. <span class="comment">document non-spec'ed parameters to document.open</span></p>
<p>Do not confuse this method with <a href="/en/DOM/window.open" title="en/DOM/window.open">window.open()</a>. <code>document.open</code> allows you to overwrite the current document or append to it, while <code>window.open</code> provides a way to open a new window, leaving the current document intact. Since <code>window</code> is the global object, just calling <code>open(...)</code> does the same as <code>window.open(...)</code>.</p>
<p>You can close the opened document using <a href="/en/DOM/document.close" title="en/DOM/document.close">document.close()</a>.</p>
<p>{{ gecko_minversion_note("1.9", "Starting with Gecko 1.9, this method is subject to the same same-origin policy as other properties, and does not work if doing so would change the document's origin.") }}</p>
<p>{{ gecko_minversion_note("1.9.2", "Starting with Gecko 1.9.2, <code>document.open()</code> uses the <a href="/Security_check_basics" title="Security check basics">principal</a> of the document whose URI it uses, instead of fetching the principal off the stack. As a result, you can no longer call <a class="internal" href="/en/DOM/document.write" title="En/DOM/Document.write"><code>document.write()</code></a> into an untrusted document from chrome, even using <a class="internal" href="/en/wrappedJSObject" title="En/WrappedJSObject"><code>wrappedJSObject</code></a>.") }}</p>
<p>See <a href="/en/Security_check_basics" title="en/Security check basics">Security check basics</a> for more about principals.</p>
<h2 id="Specification" name="Specification">Specification</h2>
<p><a class="external" href="http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-72161170">DOM Level 2 HTML: <code>open()</code> Method</a></p>
<p>{{ languages( { "fr": "fr/DOM/document.open", "ja": "ja/DOM/document.open", "pl": "pl/DOM/document.open" } ) }} </p>
Revert to this revision