Javascript Reference - HTML DOM close() Method








The close() method closes the output stream opened with the document.open() method.

Browser Support

close Yes Yes Yes Yes Yes

Syntax

document.close()

Parameters

None.

Return Value

No return value.





Example

The following code shows how to open an output stream, add some text, then close the output stream.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<script>
function myFunction()<!--from   w  w  w  .  j  ava  2s  . co m-->
{
  var doc=document.open("text/html","replace");
  var txt="<html><body>content</body></html>";
  doc.write(txt);
  doc.close();
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to open an output stream in a new window, add some text, then close the output stream.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<!--from   www  .  ja va 2  s .  c  o  m-->
<script>
function myFunction()
{
    var w=window.open();
    w.document.open();
    w.document.write("<h1>Hello World!</h1>");
    w.document.close();
}
</script>

</body>
</html>

The code above is rendered as follows: