HTML DOM close() method


The HTML DOM close() method is used to close the output stream. It is done to indicate that writing on the window has been finished. It doesn’t take any parameter and displays all the previous data before closing.

Syntax

Following is the syntax for HTML DOM close() method −

document.close()

Example

Let us see an example for the close() method −

Live Demo

<!DOCTYPE html>
<html>
<body>
<p>Click the below button to open an output stream put some text in it and finally close it.</p>
<button onclick="streamClose()">CLOSE STREAM</button>
<script>
   function streamClose() {
      document.open();
      document.write("<h1>SAMPLE HEADING</h1>");
      document.write("<h2>SAMPLE HEADING 2</h2>");
      document.write("<p>A paragraph</p>");
      document.close();
   }
</script>
</body>
</html>

Output

This will produce the following output −

On clicking CLOSE STREAM −

We have created a button CLOSE STREAM that will execute the streamclose() method on click −

<button onclick="streamClose()">CLOSE STREAM</button>

The streamClose() method uses the open method on document which opens a new stream and clears all the previous text that was written on the document. Using document.write we write a <h1> , <h2> and <p> element to the document. Finally, we close that input stream using the close() method on the document.−

function streamClose() {
   document.open();
   document.write("<h1>SAMPLE HEADING</h1>");
   document.write("<h2>SAMPLE HEADING 2</h2>");
   document.write("<p>A paragraph</p>");
   document.close();
}

Updated on: 20-Feb-2021

168 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements