Contents:
The Implicit Window Reference
Multiple Windows and Explicit Window References
Windows and Frames
Window and Frame Names
The JavaScript Name Space
Window and Variable Lifetime
Garbage Collection
The JavaScript Object Hierarchy
The interesting features of client-side JavaScript are those that integrate the programming language with the functionality of the browser. Since the most notable function of any web browser is its ability to display HTML text in a window, the Window object is the central, most important object in JavaScript. As we'll see in this chapter, the Window object is also the root of the "object hierarchy"--that is, all other HTML objects in JavaScript are accessed as properties of the Window object, or as properties of those properties. JavaScript HTML objects other than the Window object will be documented in the chapters that follow this one.
In client-side JavaScript, the web browser window is represented by a Window object. This object has methods like alert() and prompt() that pop up dialog boxes to display messages and get input from the user. It has properties like location that specify the URL of the document currently displayed in the window and also allows programs to force the window to load a new document. As further examples, the Window object also has a status property that controls the message displayed in the browser status line, and a history property that refers to an object which allows programs to move the browser backwards and forwards through the user's browsing history.
While we've named various methods and properties of the Window object, we haven't named the Window object itself yet. ("Window" is the object's type, of course, not a reference to the actual object.) In fact, the Window object simply does not have a name--that is, there is no variable that contains a reference to the object that represents the browser window. The Window object is so central to client-side JavaScript that every JavaScript expression is evaluated in the context of that object. So whenever you use properties like history or methods like alert(), you implicitly refer to the history property of the Window object and the alert() method of the Window object. This reference to the window is implicit in all JavaScript expressions.
Having said this much, you may be confused, because you've probably seen JavaScript code that uses expressions like this:
window.alert("The URL is: " + window.location);
alert("The URL is: " + location);
The Window object has another property, self, that is a synonym for the window property. In some cases, it is useful to use one of these properties to make your code clearer or to disambiguate it. Using these properties is largely a stylistic matter, however. For example, you might find it clearer to rewrite the JavaScript statement above like this:
alert("The URL is: " + self.location);