5.2. Writing to Other Frames and Windows
You can also use the document.write( ) method to send dynamically created content to another frame in a frameset or to another browser window previously opened by a script in the same page. In this case, you are not restricted to only one call to document.write( ) per page; you can open an output stream to another frame or window and keep dumping stuff into it until you close the stream with document.close( ).
All you need for this kind of content creation is a valid reference to the other frame or window. How you generate the frameset or secondary window influences this reference.
5.2.1. Framesets and Frames
A typical frameset document defines the physical layout of how the main browser window is to be subdivided into separate panels. Framesets can, of course, be nested many levels deep, where one frame loads a document that is, itself, a frameset document. The key to writing a valid reference to a distant frame is knowing the relationship between the frame that contains the script doing the writing and the target frame.
Example 5-3. A simple three-frame frameset with blank pages written to each frame
Now imagine that a modified version of Example 5-2 is loaded into the main frame. The job of the script, however, is to write the dynamic content to the frame named instrux. To accomplish this, the reference to the other frame must start with the parent document (the frameset), which the two frames have in common. Example 5-4 shows the modified page that goes into the main frame and writes to the instrux frame. The two small changes that were made to the original code are highlighted.
Example 5-4. Writing dynamic content to another frame
If, on the other hand, you simply want to load a different document from the server into the instrux frame, you can use a scriptless HTML link and set the target attribute to the instrux frame. A script in main can also specify a document for the instrux frame as follows:
parent.instrux.location.href = "nextPage.html";
5.2.2. Secondary Windows
Browser object models provide facilities for not only generating a new browser window, but also setting the window's size and (in Version 4 browsers and later) its location on the screen. You can then use references to communicate from one window to the other, although the form of those references is quite different, depending on where the script is running.
The window.open( ) method that generates a new window returns a reference to the new window object. If you plan to communicate with that window after it has been opened, you should store the reference in a global variable. This reference is the only avenue that scripts may use to access the subwindow. Example 5-5 features a script for opening a new window and writing to it. In addition, it also takes care of a feature lacking in Navigator 2 (described in a moment), inserts a brief delay to allow the often sluggish Internet Explorer for Windows to finish creating the window before writing to it, and brings an already opened but hidden window to the front, if the browser supports that feature (Navigator 3 or later and IE 4 or later).
Example 5-5. Opening a new window and writing to it
Example 5-5 shows that the reference to the subwindow (stored in the newWindow global variable) can be used to call document.write( ) and document.close( ) for that window. The newWindow object reference is the gateway to the subwindow.
A script in a document loaded into a subwindow can communicate back to the window or frame that spawned the new window. Every scriptable browser (except Navigator 2) automatically sets the opener property of a new window to a reference to the window or frame that created the window (recent browsers also set this property to windows launched by a and form element target attributes). One of the workarounds in Example 5-5 creates and sets this property for Navigator 2, so you can use it across the board. Therefore, to access the value property of a form's text box (named entryField) located in the main browser window, you can use the following script statement in the subwindow:
Remember that opener refers directly to the window or frame that spawned the subwindow. If you need to access content in another frame in the host window's frameset, your reference must traverse the object hierarchy accordingly:
Copyright © 2003 O'Reilly & Associates. All rights reserved.