Document: open() method

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

This does come with some side effects. For example:

  • All event listeners currently registered on the document, nodes inside the document, or the document's window are removed.
  • All existing nodes are removed from the document.

Syntax

js
open()

Parameters

None.

Return value

A Document object instance.

Examples

The following simple code opens the document and replaces its content with a number of different HTML fragments, before closing it again.

js
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();

Notes

An automatic document.open() call happens when document.write() is called after the page has loaded.

Content Security

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.

Three-argument document.open()

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:

js
document.open("https://www.github.com", "", "noopener=true");

Two-argument document.open()

Browsers used to support a two-argument document.open(), with the following signature:

js
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.

Specifications

Specification
HTML
# dom-document-open-dev

Browser compatibility

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
Chrome – Partial support
Chrome 1 – 44 (Release date: 2008-12-11)
footnote Partial support
footnote Only supported for HTMLDocument, not all Document objects.
Chrome – Full support
Chrome 45 (Release date: 2015-09-01)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Partial support
Firefox 1 – 68 (Release date: 2004-11-09)
footnote Partial support
footnote Only supported for HTMLDocument, not all Document objects.
Firefox – Full support
Firefox 69 (Release date: 2019-09-03)
footnote Full support
Opera – Partial support
Opera 12.1 – 50 (Release date: 2012-11-20)
footnote Partial support
footnote Only supported for HTMLDocument, not all Document objects.
Opera – Full support
Opera 51 (Release date: 2018-02-07)
footnote Full support
Safari – Partial support
Safari 1 – 10.1 (Release date: 2003-06-23)
footnote Partial support
footnote Only supported for HTMLDocument, not all Document objects.
Safari – Full support
Safari 11 (Release date: 2017-09-19)
footnote Full support
Chrome Android – Partial support
Chrome Android 18 – 44 (Release date: 2012-06-27)
footnote Partial support
footnote Only supported for HTMLDocument, not all Document objects.
Chrome Android – Full support
Chrome Android 45 (Release date: 2015-09-01)
footnote Full support
Firefox for Android – Partial support
Firefox for Android 4 – 68 (Release date: 2011-03-29)
footnote Partial support
footnote Only supported for HTMLDocument, not all Document objects.
Firefox for Android – Full support
Firefox for Android 79 (Release date: 2020-07-28)
footnote Full support
Opera Android – Partial support
Opera Android 12.1 – 46 (Release date: 2012-10-09)
footnote Partial support
footnote Only supported for HTMLDocument, not all Document objects.
Opera Android – Full support
Opera Android 47 (Release date: 2018-07-23)
footnote Full support
Safari on iOS – Partial support
Safari on iOS 1 – 10.3 (Release date: 2007-06-29)
footnote Partial support
footnote Only supported for HTMLDocument, not all Document objects.
Safari on iOS – Full support
Safari on iOS 11 (Release date: 2017-09-19)
footnote Full support
Samsung Internet – Partial support
Samsung Internet 1 – 4.2 (Release date: 2013-04-27)
footnote Partial support
footnote Only supported for HTMLDocument, not all Document objects.
Samsung Internet – Full support
Samsung Internet 5 (Release date: 2016-12-15)
footnote Full support
WebView Android – Partial support
WebView Android 4.4 – 44 (Release date: 2013-12-09)
footnote Partial support
footnote Only supported for HTMLDocument, not all Document objects.
WebView Android – Full support
WebView Android 45 (Release date: 2015-09-01)
footnote Full support
WebView on iOS – Partial support
WebView on iOS 1 – 10.3 (Release date: 2007-06-29)
footnote Partial support
footnote Only supported for HTMLDocument, not all Document objects.
WebView on iOS – Full support
WebView on iOS 11 (Release date: 2017-09-19)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
Has more compatibility info.

See also