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.
file: /Techref/language/java/script/definitive/ch11_06.htm, 6KB, , updated: 2019/10/14 15:00, local time: 2024/11/15 20:46,
18.222.116.168: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? <A HREF="http://massmind.ecomorder.com/techref/language/java/script/definitive/ch11_06.htm"> [Chapter 11] 11.6 Window and Variable Lifetime</A> |
Did you find what you needed? |
Welcome to ecomorder.com! |
Welcome to massmind.ecomorder.com! |
.