Open In App

HTML DOM close() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The DOM close() method is used to close the output stream. It is used to indicate the finish of writing on a window. The close() method does not require any parameter. 

Syntax:

document.close()

Example 1: In this example, we will use DOM close() method.

HTML




<!DOCTYPE html>
<html>
   
<head>
    <title>DOM close Method</title>
    <style>
        h1,
        h2 {
            color: green;
        }
    </style>
</head>
 
<body>
    <h1>GeeksForGeeks</h1>
    <h2>DOM close() Method</h2>
    <button onclick="geeks()">Submit</button>
    <script>
        // This function is called on submit, it opens
        // a stream, write "PRACTICE" and then closes
        // the stream.
        function geeks() {
            document.open();
            document.write("<h1>PRACTICE</h1>");
            document.close();
        }
    </script>
</body>
   
</html>


Output:

Example 2: This example uses a window.close method to show the output in the new window. 

html




<!DOCTYPE html>
<html>
   
<head>
    <style>
        h1,
        h2 {
            color: green;
        }
    </style>
</head>
 
<body>
    <h1>GeeksForGeeks</h1>
    <h2>DOM close() Method</h2>
    <button onclick="geeks()">Submit</button>
    <script>
        // This function is called on submit, it opens
        // a new window and a stream, write "PRACTICE" and
        // then closes the stream.
        function geeks() {
            let gfg = window.open();
            gfg.document.open();
            gfg.document.write("<h1>PRACTICE</h1>");
            gfg.document.close();
        }
    </script>
</body>
   
</html>


Output: 

Supported Browsers: The browser supported by DOM close() method are listed below:

  • Google Chrome 64
  • Edge 12
  • Firefox 1
  • Opera 51
  • Safari 11


Last Updated : 04 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads