Why is document.write considered a “bad practice”?

There’s actually nothing wrong with document.write, per se. The problem is that it’s really easy to misuse it. Grossly, even.

In terms of vendors supplying analytics code (like Google Analytics) it’s actually the easiest way for them to distribute such snippets

  1. It keeps the scripts small
  2. They don’t have to worry about overriding already established onload events or including the necessary abstraction to add onload events safely
  3. It’s extremely compatible

As long as you don’t try to use it after the document has loaded, document.write is not inherently evil, in my humble opinion.

Leave a Comment