JS: document.write

By Xah Lee. Date: . Last updated: .

This page shows a JavaScript example of generating HTML content dynamically using document.write.

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

for (let 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. It is a blocking construct. When browser encounters it, it stops everything and run the code. document.write() is not used in modern web applications. [see JS: Browser Window Object, Document Object Model]

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

Simple DOM Examples

  1. JS: Change CSS
  2. DOM: Change Element's Content
  3. DOM: Create HTML Element
  4. DOM: Remove HTML Element

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

Or, Buy JavaScript in Depth

Ask me question on patreon