please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 11
Windows and the JavaScript Name Space
Next
 

11.6 Window and Variable Lifetime

We've seen earlier that Window objects are the central feature of client-side JavaScript, and that all variables (except those local to functions) are actually properties of a window. Having investigated the scope of variables, we now turn to the lifetime of the Window object, and of the variables it contains. In particular, we want to look at what happens when a window or frame moves from one web page and on to another.

A Window object that represents a top-level browser window exists as long as the window it represents exists. A reference to that Window object remains valid regardless of how many web pages it loads and unloads. The Window object is valid as long as the top-level window is open.[3]

[3] As we'll see in the next section, a Window object may not actually be destroyed when its window is closed, but references to that window will no longer be of much use.

A Window object that represents a frame remains valid as long as the frame remains within the frame or window that contains it. If the containing frame or window loads a new document, then the frames it contains will be destroyed in the process of loading that new document.

This is to say that Window objects, whether they represent top-level windows or frames, are fairly persistent--their lifetimes may be longer than that of the web pages that they contain and display, and longer than the lifetime of the scripts contained in the web pages they display.

When a web page that contains a script is unloaded because the user has pointed the browser on to a new page, the script is unloaded along with the page that contains it. (If the script was not unloaded, a browser might soon be overflowing with various lingering scripts!) But what about the variables defined by the script? Since these variables are actually properties of the Window object that contained the script, you might think that they would remain defined. On the other hand, leaving them defined seems dangerous--a new script that was loaded wouldn't be starting with a clean slate, and in fact, it could never know what sorts of properties (and therefore variables) were already defined.

In fact, all user-defined properties (which includes all variables) are erased whenever a web page is unloaded. The scripts in a freshly loaded document start with no variables defined, and no properties in their Window object, except for the standard properties defined by the system. What this means is that the lifetime of scripts and of the variables they define is the same as the lifetime of the document that contains the script. This is potentially much shorter than the lifetime of the window or frame that displays the document that contains the script.


Previous Home Next
The JavaScript Name Space Book Index Garbage Collection

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

file: /Techref/language/java/script/definitive/ch11_06.htm, 6KB, , updated: 2019/10/14 15:00, local time: 2024/11/23 12:53,
TOP NEW HELP FIND: 
3.142.98.60: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_06.htm"> [Chapter 11] 11.6 Window and Variable Lifetime</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!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .