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, DOM]

DOM How-To


Web Scripting Overview


HTML Input


Web Scripting Examples


Web Scripting Misc


jQuery


node.js

Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

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

Web Dev Tutorials