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 document.open() 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.
Beware if you use document.write() at any time other than during the document parsing. If you use it in an event handler, you will overwrite the entire documents. Once you have overwritten the document, even the event handler has gone and there is no trace of the original document so when you subsequently refresh again, you may get a blank page.
Destroying the document content with a document.write() will crash Netscape 2 browsers. It may lead to unexpected behavior in Netscape 3 browsers.
Beware that if you were to use a with statement to add the document object to the scope chain, your script would look very much like the server side scripts which use the write() method on its own.
If the output that you write does not immediately appear, it may have been buffered by the browser. There is no way to flush this buffer out to the screen and keep the document open. The only way to flush the buffer is to call the document.close() method. However any subsequent document.write() will implicitly call document.open() and clear the page as it starts a new empty document.
If you need to change the HTML at that time, you will need to resort to the following dynamic HTML techniques:
-<DIV> blocks
-<SPAN> blocks
-innerHTML and innerText properties of elements
-createElement() and its related DOM support.
Prev | Home | Next |
Document.width | Up | Document.writeln() |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |