7
I saw in a certain answer from SOEN a question about printing iframe content.
I ended up encountering an excerpt 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 that one was document.close
. Then I ended up looking at the W3schools.
There is an example of using document.open
and document.close
, thus:
<!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 both with document.close
or without, nothing different happens in the examples (the same goes for document.open
).
See without the 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 the 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, after all, what’s the point 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 really have to use one or the other?