JavaScript: 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 Browser Window Object]

βˆ‘ JS in Depth
XAH Β BUY NOW

JS in Depth

JS Obj Ref

DOM


DOM

Overview

Basic Examples

HTML Input

Web Scripting Examples

Web Scripting Misc

jQuery

node.js