home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


JavaScript: The Definitive Guide

Previous Chapter 11 Next
 

11. Windows and the JavaScript Name Space

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.

11.1 The Implicit Window Reference

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);
This is how it works: the Window object actually has a property named window that refers to itself. Thus, the expressions above are still implicitly evaluated in the context of the Window object. They reference the window property, which is simply another reference, explicit this time, to the same Window object. Then these expressions use this explicit reference to refer to the alert() method or location property. Therefore, using window in the above expression is unnecessary, and the following would work just as well.

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);
There are also a few occasions in which you need an explicit reference to the Window object--if you want to pass it as an argument to a function, for example. The self and window properties are useful in these cases.


Previous Home Next
JavaScript and Threads Book Index Multiple Windows and Explicit Window References

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell