You are here: Reference > JavaScript > client-side > HTML DOM > methods > open (document)

open method (document)

Browser support:
Opens a document stream for writing.
If more than two parameters are set for the document.open method, then it works like the window.open method. This page describes that case only if at most two parameters are set for the document.open method. For other cases, please see the page for the window.open method.
If at most two parameters are specified, then the open method opens the output stream for writing. When the document stream is opened, the write and writeln methods can be used to write some content into the document. If the document was opened by the open method, the close method must be used to close the output stream.
Note: while the document is loading, the document stream is opened for writing. In that case, there is no need to open and close the output stream.
After the document has been loaded, the open method (if at most two parameters are specified) clears the current document. In Internet Explorer, the cleared document is placed in the history list before or after the current item depending on the second parameter of the open method.

Syntax:

object.open ([MIMEtype [, historyPosition]]);
You can find the related objects in the Supported by objects section below.

Parameters:

MIMEtype
Optional. String that specifies a MIME type for the document.
One of the following values:
text/html
Deafult. HTML formatted text.
text/plain
Plain text format.
image/gif
GIF image format.
image/jpeg
JPEG image format.
image/xbm
XBM image format.
plugIn
Plugin.
historyPosition
Optional, only supported by Internet Explorer. String that specifies the position of the opened (and cleared) document in the history list. If not specified, the opened document will be placed after the current item in the history list.
Only the following value is supported:
replace
Indicates that the opened document will be placed before the current item in the history list.

Return value:

Returns the opened (and cleared) document object (it is always the same as the current document).

Example HTML code 1:

This example illustrates the use of the open, write and close methods to replace the contents of the document:
<head>
    <script type="text/javascript">
        function ReplaceContent () {
            document.open ("text/html");
            document.write ("<b>Bold text</b>");
            document.write ("<i>Italic text</i>");
            document.close ();
        }
    </script>
</head>
<body>
    <button onclick="ReplaceContent ();">Replace the contents of the document</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content