-3

Why is it generally not considered good practice to use document.write in Javascript? I know it is not the most elegant or best way to do it, but are there any true errors with it? Is it ever ok? And in what case would it be better than inner.HTML

I understand that it will rewrite the document, if used after the loading. However, should I be using innerHTML even if I am trying to rewrite the document? If so, why?

I know some have asked if I even looked at the related questions before posting; I did. But I did not find anything satisfactory.

tjons
  • 4,460
  • 5
  • 25
  • 36
  • Your answer is here I guess, it is about performance. http://stackoverflow.com/a/13499903/1517542 – Sarge Sep 13 '13 at 14:49
  • 2
    downvoting for the lack of research. Did you even look the related question list before posting? – John Dvorak Sep 13 '13 at 14:54
  • Yes, I did look at it. I think my wording was strange, or something, because I saw nothing like it. – tjons Sep 13 '13 at 15:00

6 Answers6

5

When used correctly, it's fine, but of very limited use.

You can't use it in XHTML, but I would recommend not using XHTML anyway unless you have a really, really good reason to.

The problem comes when you call document.write after the initial page load is complete. During page load is fine, but afterward, it causes an implicit document.open, which clears the document from the window and starts fresh.

Using document.write also means you can't demand-load your scripts or use the async or defer attributes on your script elements.

Finally, I usually recommend putting your JavaScript in a separate (single) JavaScript file at the very end of the document (just before the closing </body> tag). If you want to use document.write to output to the page, you'd have to include that script file higher up or use code directly within the HTML, neither both of which I recommend against.

So since it's of such limited use compared with other methods, it's best to get proficient with the other methods.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
2

yeah, calling document.write after the document has finished loading will wipe the document. Fire up your console, and do a document.write("test") call. The page will get overwritten with this new content. Except maybe for setting iframe innerHTML in a single step, never use document.write.

Mike 'Pomax' Kamermans
  • 40,046
  • 14
  • 84
  • 126
1

One reason can be that document.write does not work in XHTML

Also document.write only works while the page is loading; If you call it after the page is done loading, it will overwrite the whole page ie, after your document has been full rendered and closed, using document.write() will clear your current document and start a new one, wiping out all previous content.

Rahul Tripathi
  • 152,732
  • 28
  • 233
  • 299
1

From the MDN :

Writing to a document that has already loaded without calling document.open() will automatically perform a document.open call. Once you have finished writing, it is recommended to call document.close(), to tell the browser to finish loading the page. The text you write is parsed into the document's structure model. In the example above, the h1 element becomes a node in the document.

In short : code that must be executed past the initial load (for example in an event handler) can't include a document.write or they trigger a replacement of the whole document.

Denys Séguret
  • 335,116
  • 73
  • 720
  • 697
1

Because it replaces entire document. In 99% of cases you just need to replace content of some specific elements.

document.write does have it uses - for example when you need to write an executable script to a newly created document.

Yuriy Galanter
  • 35,167
  • 11
  • 60
  • 119
1

As a good pratice, sometimes we have information on the client side and using javascript we write these informations in div and span elements because they are in the right position of the html layout. And as the guys said, document.write does not work with xhtml.

Felipe Oriani
  • 35,246
  • 17
  • 121
  • 176