Manipulating Frames in Script

The Frames object is a collection of all of the frame windows in the current frameset. Each frame is a Window object in itself, and has its own Document object to represent the HTML in the frame. This really isn’t as confusing as it might sound—it mimics the organization of frames without an object model. The top-level frame subdivides the browser window and loads separate HTML pages for each frame.

It can be confusing to use this property though. By default the Window and Document object that a script refers to are the Window and Document objects of the current frame. This means that, if we execute a script in the left-hand frame of a two-frame window, the Document, Window, and Frames objects the script uses will be those of the left-hand frame. Since the left-hand frame has no sub-frames, examining the Count property of the Frames collection will return 0.

However, if we use the Top (or, in this case, Parent) property of the left-hand frame’s Window object, we can gain access to the top-level frameset that loaded both the frames. Examining the Count property of this Frames collection will show the expected value of 2, because this top-level page has loaded two frames.

We’re giving this brief coverage here because it’s both a simple and complex topic. In the sense that the Frames collection has only one property, Count, and that it just provides access to other objects that we’ve covered before, like Window and Document, it is a simple object. However, the relationships between the different frames in a frameset can be complex, and to best understand how it works, we should really see some code. A more in-depth example of using frames with the object model is part of the next chapter.