Приглашаем посетить
История (med.niv.ru)

The Window Object

The window Object

As mentioned, the window object is the top-level object in Dynamic HTML. The easiest way to understand the window object is to think of it as a container for a document or for other windows. A window containing other windows is the basis of a frameset. Figure 4-1 shows the window object hierarchy.

The Window Object

Figure 4-1. The object hierarchy for the window object.

The window object maintains information about the browser and exists as long as the browser's application window exists. This means that as the user browses from page to page, the window object remains available, even though the current document changes.

Referencing the window Object

Because the window object is the top-level object in the HTML object model, it does not have to be explicitly referenced when you are accessing the properties of the window. For example, the following two lines of code are effectively the same:

window.location.URL  // Explicitly reference the window object.
location.URL         // The window object is implicitly referenced.

In addition, the window object exposes a self property that actually returns the window. Therefore, the following five lines of code reference the same name property:

name
self.name
window.self.name
window.self.window.name
window.window.name

Implicit window references work only for code that references the current window. To reference other, noncurrent windows or frames, the particular window objects must be explicitly referenced.

The document and event Properties

The document property returns a document object representing the page contained within the window. Through the document property, the style, structure, and contents of the contained document can be accessed.

As mentioned in Chapter 3, "Dynamic HTML Event Model," the event property of the window object returns an event object, which provides information about the current event. The event object is accessible only during an event sequence and returns null at all other times. It is possible to respond to events that occur in other windows or documents; Chapter 5, "Window and Frame Management," explains how.

Global Variables and User-Defined Properties

As mentioned in Chapter 2, "Fundamentals of HTML Scripting," no global variables are available when you are scripting in Dynamic HTML. Instead, all variables declared outside the scope of a function or an event handler are automatically added as user-defined properties of the window object. When the user exits a page, variables that were added by the page are removed from the window. This is done for a number of reasons: so that a new page can be certain that no properties yet exist on the page, and for security purposes, so that another page does not come along and attempt to read the state of the prior page.

Therefore, the lifetime of the user-defined properties of the window object is the same as the lifetime of the script, even though the window object exists until the application window is destroyed. When a new page is loaded, the only exposed window object properties are the built-in properties defined by Dynamic HTML.

Naming the Window

Each window is created without a name. You can name a window by assigning a string to its name property. You can supply a name for a frame when you create it as part of a frameset.

The name property designates the target for a link anchor or form results. By default, all pages are targeted to the current frame or to the frame or window specified by the <BASE TARGET=windowName> tag. You can override this target by supplying a TARGET attribute to a link anchor or Form element, specifying which named window the document should appear in.

The window object's name property is retained by the browser as the user navigates to a page in order to ensure that frame targeting is maintained.

Evaluating Strings as Code

The window object exposes an eval method that can evaluate a passed-in string as code and return the result. The code is executed in the context of the currently executing scripting language.

[Содержание]