I'm testing some JavaScript code, and realized that this code...
var msg = "Hello, World!";
document.open();
document.write(msg);
document.close();
...has the same result as this one:
var msg = "Hello, World!";
document.write(msg);
What's the difference?
Much behaviour around document.write was established before formal specifications so behaviour is inconsistent and somewhat arbitrary across browsers. However, behaviour is now fairly consistent but there are differences depending on when it's called.
The use of document.write is largely discouraged, however it's still useful. It's ubiquitous support means it can be used as a replacement for other techniques if really old browsers need to be accommodated.
While a document is loading
If you use document.write while the document is loading (e.g. a script element in the document source), then there is no need to call open or close. The document is opened when navigated to and closed when the content has finished loading (i.e. when the load event occurs). So as long as all the write statements are executed before load occurs, the browser will do the rest.
After window load event dispatched
Once the document has finished loading (e.g. the load event has been dispatched), then a call to document.write will first call clear which will clear the entire content of the document, everything. In this case, not all browsers will automatically call close when the call to write ends.
Some browsers take a guess and seem to call close at some later time (IE?), others (Firefox, Safari) will keep the document open, which might cause some unusual behaviour.
Child windows
If you open a child window, e.g. using window.open, then write to it from the parent, the write will occur after the page has finished loading so it will clear the document. e.g.
In this case you'll never see Google, the call to write clears the page the moment it loads and writes the new content.
Also, browsers won't automatically call close, you can make subsequent calls to document.write and they will append to the existing markup, e.g.
You may see some animation on the tab or window showing that it's still loading.
If, in the above, openWindow calls document.close before ending, then the subsequent call to writeToWindow will clear the document first so that the div is the only element in the document (along with the mandatory HTML, HEAD and BODY elements automatically added by the browser and probably a TITLE added by error correction).
Therefore, you should call close at an appropriate point in this case.
The OP
If the following is called during load, then:
So only the document.write line does anything useful in this case.
Some play code:
heres a fiddle example http://jsfiddle.net/8stv489e/
with this code
As you can see in the fiddle, the document contents are overwritten as the document is reinitialized on
open().
read this https://developer.mozilla.org/en-US/docs/Web/API/document.open
while
document.close
need not be done anywhere explixitly. document.write automatically closes the streamThe difference between
open()
andwrite()
is thatopen()
clears the document so you can write to it.write()
actually puts stuff in the document.Explicitly calling
document.open()
/document.close()
is not necessary asdocument.write()
implicitly handlesopen()
when the page has loaded andclose()
when finished.Read the documentation here