The Document.open() method opens a document for writing.
This does come with some side effects. For example:
open()
None.
A Document object instance.
The following simple code opens the document and replaces its content with a number of different HTML fragments, before closing it again.
document.open();
document.write("<p>Hello world!</p>");
document.write("<p>I am a fish</p>");
document.write("<p>The number is 42</p>");
document.close();
An automatic document.open() call happens when document.write() is called after the page has loaded.
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.
There is a lesser-known and little-used three-argument version of document.open(), which is an alias of Window.open() (see its page for full details).
This call, for example opens github.com in a new window, with its opener set to null:
document.open("https://www.github.com", "", "noopener=true");
Browsers used to support a two-argument document.open(), with the following signature:
document.open(type, replace);
Where type specified the MIME type of the data you are writing (e.g. text/html) and replace if set (i.e., a string of "replace") specified that the history entry for the new document would replace the current history entry of the document being written to.
This form is now obsolete; it won't throw an error, but instead just forwards to document.open() (i.e., is the equivalent of just running it with no arguments). The history-replacement behavior now always happens.
| Specification |
|---|
| HTML> # dom-document-open-dev> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
open |
45 | 12 | 69 | 51 | 11 | 45 | 79 | 47 | 11 | 5.0 | 45 | 11 |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Document/open