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.
<!--Driver Code Starts-->
<!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>
<!--Driver Code Ends-->
<button onclick="geeks()">Submit</button>
<script>
function geeks() {
document.open();
document.write("<h1>PRACTICE</h1>");
document.close();
}
</script>
<!--Driver Code Starts-->
</body>
</html>
<!--Driver Code Ends-->
Example 2: This example uses a window.close method to show the output in the new window.Â
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>
<head>
<style>
h1,
h2 {
color: green;
}
</style>
</head>
<body>
<h1>GeeksForGeeks</h1>
<h2>DOM close() Method</h2>
<!--Driver Code Ends-->
<button onclick="geeks()">Submit</button>
<script>
function geeks() {
let gfg = window.open();
gfg.document.open();
gfg.document.write("<h1>PRACTICE</h1>");
gfg.document.close();
}
</script>
<!--Driver Code Starts-->
</body>
</html>
<!--Driver Code Ends-->
- Closes a script opened document stream : After document.open() and one or more document.write() calls, document.close() inserts an explicit end-of-file marker so the browser finishes parsing and renders the newly written content.
- Has no effect (and is silent) if no open stream exists : Calling it on a normal page that was never opened with document.open() does nothing and raises no error; it only acts when a script-created parser is active.