please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 11 Next
 

11. Windows and the JavaScript Name Space

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.

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

file: /Techref/language/JAVA/SCRIPT/definitive/ch11_01.htm, 8KB, , updated: 2019/10/14 15:00, local time: 2024/12/1 19:07,
TOP NEW HELP FIND: 
52.14.100.101:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://techref.massmind.org/Techref/language/JAVA/SCRIPT/definitive/ch11_01.htm"> [Chapter 11] Windows and the JavaScript Name Space</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to techref.massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .