I saw in a given response from SOEN a question about how to print iframe content.
I ended up with a snippet of code where I had the following:
var newWin = window.frames["printf"];
newWin.document.write('<body onload="window.print()">dddd</body>');
newWin.document.close();
I was curious to know what this document.close
was. So I ended up looking at W3Schools .
Here is an example of using document.open
and document.close
, like this:
<!DOCTYPE html>
<html>
<body>
<p>Click the button to open an output stream, add some text, and close the output stream.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
document.open();
document.write("<h1>Hello World</h1>");
document.close();
}
</script>
</body>
</html>
But I noticed that with either document.close
or without, nothing different happens in the examples (the same goes for document.open
).
See without document.open
:
<!DOCTYPE html>
<html>
<body>
<p>Click the button to open an output stream, add some text, and close the output stream.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
document.write("<h1>Hello World</h1>");
document.close();
}
</script>
</body>
</html>
See without document.close
:
<!DOCTYPE html>
<html>
<body>
<p>Click the button to open an output stream, add some text, and close the output stream.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
document.open();
document.write("<h1>Hello World</h1>");
}
</script>
</body>
</html>
So, what is the purpose of document.open
or document.close
?
I think that besides not being clear to me the use seems to be of no use.
Is there a case where I actually have to use one or the other?