
Client-side JavaScript
|
31
script is running in is the global object for that script, and
you can use all the properties and methods of that Window
object as if they were globally defined. When a script running
in one window needs to control or interact with a different
window, however, you must explicitly specify the Window
object:
// Create a new window and manipulate it
var w = open("newdoc.html");
w.alert("Hello new window");
w.setInterval("scrollBy(0,1)",50);
HTML allows a single window to have multiple frames.
Many web designers choose to avoid frames, but they are still
in fairly common use. JavaScript treats each frame as a sepa-
rate Window object, and scripts in different frames run inde-
pendently of each other. The
frames property of the Window
object is an array of Window objects, representing the sub-
frames of a window:
// Scripts in framesets refer to frames like this:
frames[0].location = "frame1.html";
frames[1].location = "frame2.html";
// With deeply nested frames, you can use:
frames[1].frames[2].location = "frame2.3.html";
// Code in a frame refers to the top-level window:
top.status = "Hello from the frame";
The parent property of a Window object refers to the contain-
ing frame or window. The
top property refers to the top-level
browser window that is at the root of the frame hierarchy. (If
the Window object represents a top-level window rather than
a frame, the
parent and top properties ...