JS: document.write

By Xah Lee. Date: . Last updated: .

document.write insert string into the HTML page, before browser finishes reading the rest of HTML page.

The following code prints out numbers from 1 to 9 directly into the page where the JavaScript code is:


for (var i=1; i< 10; i++) { document.write(i + " "); }


This is a simple example of inserting text into a document. But this is not a good way.

document.write() is from 1996, when DOM didn't exist yet. It is a blocking construct. When browser encounters it, it stops everything and run the code. document.write() is not used in modern web programing since about 2010.

For a much better way, see: DOM: Create/Insert HTML Element.

[see JS: Browser Window Object, Document Object Model]

JavaScript Fancy UI Examples

  1. JS: Image Rollover
  2. JS: Pop-up New Window
  3. JS: Digital Clock
  4. JS: Stopwatch
  5. JS: Fade a Element
  6. JS: Fade a Element Using CSS Transition
  7. JavaScript UI: Shake Element
  8. JS: How to Create Tooltip
  9. JS: Falling Snow Effect
  10. JavaScript Floating Box Following Scroll
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

Ask me question on patreon