Javascript Reference - HTML DOM open() Method








The open() method opens an output stream to receive the output from any document.write() or document.writeln() methods.

Syntax

document.open(MIMEtype,replace)

Parameter

Parameter Description
MIMEtype Optional. The type of document you are writing to. Default value is text/html
replace Optional. If set, the history entry for the new document inherits the history entry from the owner document

Browser Support

open Yes Yes Yes Yes Yes




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()<!--   w ww .  jav  a  2s. c o m-->
{
  var doc=document.open("text/html","replace");
  var txt="<html><body>this is a test</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, add some text, then close the output stream.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<!--from  w  ww.j  a  v  a2  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: