Document.write() (Method)

A method for writing HTML into the document body.


DOM level - 1
JavaScript - 1.0
JScript - 1.0
Internet Explorer - 3.02
Netscape - 2.0
Opera - 3.0
JavaScript syntax:-myDocument.write(anArgument)
-myDocument.write(anArgument, ...)
Argument list:anArgumentA value to be written out to the document

In a client-side script, you would use document.write() to generate some HTML.

This is a host method. It belongs to the Documentobject. Its argument values are converted to a string value and are then appended to the content of the document window and then interpreted as HTML.

This is the primary means of generating HTML as a document is parsed and the scripts are executed. Everything that is output by the document.write() method is streamed into the page at the execution point where it is called. That means if you place a <SCRIPT> block in the middle of the page and it has a document.write() in its global code (that is; not inside a function declaration), the output will be inserted into the document in place of the <SCRIPT> block.

This is a useful technique for creating dynamically changing pages where the dynamism happens at the client end.

Although writing to the current document in an event handler will destroy the document, you can perform document.write() actions in other windows to replace their document content. You will need to invoke the document.write() method belonging to the target window or frame you want the write to happen in. Here is a document.write() that is targeted at a frame somewhere in a frame-set:

top.frames[4].document.write("Some target content");

When writing across frames like this, you should call the method before the document.write() and then call document.close() afterwards. The animated browser loading icon will continue to revolve until you close the target document.

The document.write() method takes a variable number of arguments which it will concatenate in the output. You can do the concatenation manually but all that is needed is to comma separate the individual items for the document.write() to perform this step automatically.

The DOM level 1 specification suggests that this method may be deprecated in the future.


-<DIV> blocks

-<SPAN> blocks

-innerHTML and innerText properties of elements

-createElement() and its related DOM support.

See also:Comma operator (,), Document object, Document.close(),, Input-output, response.write()