↑
Main Page
object always
</head>
<frameset rows=”100,*”>
<frame src=”frame.htm” name=”topFrame” />
<frameset cols=”50%,50%”>
<frame src=”anotherframe.htm” name=”leftFrame” />
<frame src=”yetanotherframe.htm” name=”rightFrame” />
</frameset>
</frameset>
</html>
This code creates a frameset with one frame across the top and two frames underneath. Here, the top
frame can be referenced by
window.frames[0]
or
window.frames[“topFrame”]
, however, you would
probably use the
top
object instead of
window
to refer to these frames (making it
top.frames[0]
, for
instance).
The
top
object always points to the very top (outermost) frame, which is the browser window itself. This
assures that you are pointing to the correct frame. If you then write code within a frame, the
window
object referenced in it is a pointer to just that frame.
Because the window object is the center of the BOM universe, it enjoys a special privilege: You don’t
need to explicitly reference it. Whenever a function, object, or collection is referenced, the interpreter
always looks to the
window
object, so
window.frames[0]
can be rewritten as just
frames[0]
. To
understand the various ways to reference the frames in the previous example, refer to Figure 5-4.
Figure 5-4
It is also possible to access a frame directly using its name, such as
window.leftFrame
. However,
using the
frames
collection is generally more acceptable because it more accurately represents the
code’s intent.
Another instance of the window object is called
parent
. The
parent
object is used with framesets that
load files that are also framesets. Suppose the file named
frameset1.htm
contains this code:
137
JavaScript in the Browser
08_579088 ch05.qxd 3/28/05 11:37 AM Page 137
Free JavaScript Editor
Ajax Editor
©
→