HTML DOM Document open() method

Definition and usage

open() This method opens a document for writing.

Note:All existing document content will be cleared.

Tip:Do not confuse this method with opening a new browser window by window.open() method Confusion.

See also:

Document close() method

Document write() method

Document writeln() method

Example

Example 1

Open this document, write some text, and then close:

document.open();
document.write("<h1>Hello World</h1>");
document.close();

Try it yourself

Example 2

Use document.open() in a new window:

const myWindow = window.open();
const myWindow.document.open();
const myWindow.document.write("<h1>Hello World!</h1>");
const myWindow.document.close();

Try it yourself

Example 3

If document.write() is used on a closed document, document.open() will be called automatically. This will delete the existing content.

document.write("<h1>Hello World!</h1>");

Try it yourself

Syntax

document.open()
document.open(Mimetype, replace)

Parameter

Parameter Description
Mimetype Ignored by all modern browsers.
replace Ignored by all modern browsers.

Return value

None.

Browser support

document.Open() It is a DOM Level 1 (1998) feature.

All browsers support it:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support 9-11 Support Support Support Support