In tutorials I've learnt to use document.write
. Now I understand that by many this is frowned upon. I've tried print()
, but then it literally sends it to the printer.
So what are alternatives I should use, and why shouldn't I use document.write
? Both w3schools and MDN use document.write
.
I fail to see the problem with
document.write
. If you are using it before theonload
event fires, as you presumably are, to build elements from structured data for instance, it is the appropriate tool to use. There is no performance advantage to usinginsertAdjacentHTML
or explicitly adding nodes to the DOM after it has been built. I just tested it three different ways with an old script I once used to schedule incoming modem calls for a 24/7 service on a bank of 4 modems.By the time it is finished this script creates over 3000 DOM nodes, mostly table cells. On a 7 year old PC running Firefox on Vista, this little exercise takes less than 2 seconds using
document.write
from a local 12kb source file and three 1px GIFs which are re-used about 2000 times. The page just pops into existence fully formed, ready to handle events.Using
insertAdjacentHTML
is not a direct substitute as the browser closes tags which the script requires remain open, and takes twice as long to ultimately create a mangled page. Writing all the pieces to a string and then passing it toinsertAdjacentHTML
takes even longer, but at least you get the page as designed. Other options (like manually re-building the DOM one node at a time) are so ridiculous that I'm not even going there.Sometimes
document.write
is the thing to use. The fact that it is one of the oldest methods in JavaScript is not a point against it, but a point in its favor - it is highly optimized code which does exactly what it was intended to do and has been doing since its inception.It's nice to know that there are alternative post-load methods available, but it must be understood that these are intended for a different purpose entirely; namely modifying the DOM after it has been created and memory allocated to it. It is inherently more resource-intensive to use these methods if your script is intended to write the HTML from which the browser creates the DOM in the first place.
Just write it and let the browser and interpreter do the work. That's what they are there for.
PS: I just tested using an
onload
param in thebody
tag and even at this point the document is stillopen
anddocument.write()
functions as intended. Also, there is no perceivable performance difference between the various methods in the latest version of Firefox. Of course there is a ton of caching probably going on somewhere in the hardware/software stack, but that's the point really - let the machine do the work. It may make a difference on a cheap smartphone though. Cheers!Try to use getElementById() or getElementsByName() to access a specific element and then to use innerHTML property:
Here is code that should replace document.write in-place:
The reason that your HTML is replaced is because of an evil JavaScript function:
document.write()
.It is most definitely "bad form." It only works with webpages if you use it on the page load; and if you use it during runtime, it will replace your entire document with the input. And if you're applying it as strict XHTML structure it's not even valid code.
the problem:
-- quote from the MDN
document.write()
has two henchmen,document.open()
, anddocument.close()
. When the HTML document is loading, the document is "open". When the document has finished loading, the document has "closed". Usingdocument.write()
at this point will erase your entire (closed) HTML document and replace it with a new (open) document. This means your webpage has erased itself and started writing a new page - from scratch.I believe
document.write()
causes the browser to have a performance decrease as well (correct me if I am wrong).an example:
This example writes output to the HTML document after the page has loaded. Watch
document.write()
's evil powers clear the entire document when you press the "exterminate" button:the alternatives:
.innerHTML
This is a wonderful alternative, but this attribute has to be attached to the element where you want to put the text.Example:
document.getElementById('output1').innerHTML = 'Some text!';
.createTextNode()
is the alternative recommended by the W3C.Example:
var para = document.createElement('p'); para.appendChild(document.createTextNode('Hello, '));
NOTE: This is known to have some performance decreases (slower than
.innerHTML
). I recommend using.innerHTML
instead.the example with the
.innerHTML
alternative: