advertisement -- please support sponsors

dynamic content, frames:
writing text from one frame to another

How can a script in one frame write text into another frame?

To write text from one frame to another, you must be able to access the document object associated with the destination frame. This object contains several methods that can be used to write dynamic text into the destination frame without explicitly loading a new document.

methods of the document object

The document object has four methods that are useful for dynamically writing text to a document.
document . open ()
Before a document can be written to, it must first be opened. The open method erases the current document so that the next write statement can start with a "clean slate." With no parameters, the open method defaults to MIME-type "html/text," which means that subsequent write statements should send HTML-formatted text to the document.

document . write ()
document . writeln ()
The write and writeln methods output text to the document. The only difference between these two methods is that writeln outputs a newline character after the text. In most cases, however, it doesn't matter which one you use, since newline characters are significant only in certain contexts (such as preformatted text).

document . close ()
If text is written to the document and the browser doesn't know whether more is coming, the browser may not be able to display the last line of text written. This is because Netscape displays web pages line by line, not character by character, and cannot show the last line of text unless it is knows the entire line has been received. If more text will be appended later but the current line needs to be displayed immediately, the script should write a <br> tag, or some other line-ending element. However, if a script is finished writing to the document, then it should just call the close method, which signals the end of the line and the document.
In certain circumstances, calls to some of these methods generate automatic calls to other methods.

accessing the destination document object

You can access any frame's document object as a property of its window object. Thus, any frame can output text to another frame simply by calling methods in the destination frame's document object.

For example, consider the following frameset code, which divides the browser window into two, side-by-side frames:

<frameset cols="*,*"> <frame name=left src="a.html"> <frame name=right src="b.html"> </frameset>
A script in a.html (left frame) that outputs text in the right frame might appear as follows:
top . right . document . open (); top . right . document . writeln ("Hello."); top . right . document . close ();
In this example, top is a window reference to the whole window, and top.right is a window reference to the right frame. When this script is executed, the content from b.html is replaced by the new text: "Hello."

This article provides only a brief overview of how one frame can write to another. If you plan on writing serious web applications that use this feature, you should study Netscape's documentation for the method in their JavaScript Guide.

Charlton Rose
22 February 1996