Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

HTML - DOM Document open() Method



HTML DOM document open() method opens a document for writing. All existing contents of the document will be cleared.

Syntax

document.open();

Parameter

This method accepts two parameters as listed below but both of them are optional.

Parameters Description
type It specifies the type of document. It's default value is set to "text/html".
replace It specifies that the history entry for the new document would replace the current history entry of the document.

Return Value

It returns instance of document object.

Examples of HTML DOM Document 'open()' Method

The following examples illsutrates open() method to open a document for writing.

Open a Document

In the following example open() method is used to open, write and close the document.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM document open() Method
    </title>
</head>
<body>
    <button onclick="fun()">Click me</button>
    <script>
        function fun() {
            document.open();
            document.write("Welcome to Tutorials Point...");
            document.close();
        }
    </script>
</body>
</html>

Open a Document in New Window

In the following example open() method is used to open, write and close the document in a new window.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM document open() Method
    </title>
</head>
<body>
    <p>Click to open a document in new window.</p>
    <button onclick="fun()">Click me</button>
    <script>
        function fun() {
            let x = window.open()
            x.document.open();
            x.document.write("Welcome to Tutorials Point...");
            x.document.close();
        }
    </script>
</body>
</html>

Supported Browsers

Method Chrome Edge Firefox Safari Opera
open() Yes 64 Yes 12 Yes 1 Yes 11 Yes 51
html_dom_document_reference.htm
Advertisements