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.document
object has four methods that are useful for dynamically writing text to a document.
document . open ()
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 ()
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 ()
<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.
write
/writeln
methods are called but the document is not yet open, the open
method will automatically be called (and the current document cleared).
open
method is called but the document is already open, the close
method will be called before the document is re-opened.
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:
A script in a.html (left frame) that outputs text in the right frame might appear as follows:
In this example,top . right . document . open (); top . right . document . writeln ("Hello."); top . right . document . close ();
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 window.open
method in their JavaScript Guide.
Charlton Rose
22 February 1996