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:

document.write("<p>");

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

document.write("<\/p>");

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

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS Basics
  4. JS in Depth
  5. JS Reference
  6. DOM
  7. SVG
  8. JS Misc