HTML DOM open()Method

Document 对象参考手册Document Object

Definition and Usage

The open() method opens an output stream to collectdocument.write()ordocument.writeln()the content output by the method.

After calling the open() method to open a new document and using the write() method to set the document content, you must remember to use document.close()the method to close the document, and force its content to be displayed.

Note:If the target file already exists, it will be cleared. If this method has no parameters, a new window (about:blank) will be displayed.

Syntax

document.open(MIMEtype,replace)

Parameters Description
MIMEtype Optional. Specifies the type of document being written. The default value is "text/html".
replace Optional. When this parameter is set, it can cause the new document to inherit history entries from the parent document.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the open() method


Example

Example

Open an output stream, add text, then close the output stream:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Rookie Tutorial (example.com)</title>
<script>
function createDoc(){
    var doc=document.open("text/html","replace");
var txt="<!DOCTYPE html><html><body>Learning HTML DOM is very interesting!</body></html>";
    doc.write(txt);
    doc.close();
}
</script>
</head>

<body>
<input type="button" value="New document" onclick="createDoc()">
</body>
</html>

Try it Yourself »

Example 2

Open an output stream (a new window; about:blank), add text, then close the output stream:

<html>
<body>

<script>
var w=window.open();
w.document.open();
w.document.write("<h1>Hello World!</h1>");
w.document.close();
</script>

</body>
</html>

Try it Yourself »


Document 对象参考手册Document Object Other Extensions