JavaScript: The Definitive Guide

Previous Chapter 11
Windows and the JavaScript Name Space
Next
 

11.4 Window and Frame Names

The second, optional[1] argument to the open() method discussed earlier is a name for the newly created window. By giving a top-level browser window a name, we've seen that you can look up a reference to that window by calling the open() method again. But you can also refer to a window by name in another way: by specifying the window name as the value of the TARGET attribute of the <A>, <MAP>, and <FORM> tags. What this does is tell the browser where you want the results of activating a link, clicking on an image map, or submitting a form to be displayed. For example, if you have two windows, one named "table_of_contents" and the other named "mainwin", then you might have HTML like the following in the "table_of_contents" window:

<A href="chapter01.html" TARGET="mainwin">
Chapter 1, Introduction
</A>
When the user clicks on this hyperlink, the browser will load the specified URL, but instead of displaying it in the window the link is in, it will display it in the window named "mainwin". If there is no window with the name "mainwin", then clicking on the link will create a new window with that name, and load the specified URL into it.

[1] This argument is not optional in Internet Explorer 3.0.

Since frames are a type of window, frames can also have names that can be used with the TARGET attribute. You specify a name for a frame with the NAME attribute of the <FRAME> tag that creates the frame.

There is even another reason to give names to frames. We've seen that every Window object has a frames[] array that contains references to each of its frames. This array contains all frames in a window (or frame) whether or not they have names. But if a frame is given a name, then a reference to that frame is also stored in a new property of the parent Window object. The name of that new property is the same as the name of the frame. Therefore, if you create a frame with HTML like this:

<FRAME NAME="table_of_contents" src="toc.html">
Then you can refer to that frame from another, sibling frame with:

parent.table_of_contents
This makes your code easier to read and understand than using (and relying on) a hardcoded array index as you'd have to do with an unnamed frame:

parent.frames[1]

Much of the discussion in this section has been about the TARGET attribute and other features of HTML rather than about JavaScript itself. If windows can have names, then it is logical to expect that Window objects have a JavaScript property that contains the window name. This is indeed true. The name property of any Window object contains the name of that window. In Navigator 2.0, this property is read-only. In Navigator 3.0, however, you can set this property, thereby changing the name of a window or a frame. One common reason to do this is to set the name of the initial browser window. When Navigator starts up, the initial window has no name, and so it cannot be used with the TARGET attribute. If you set the name property of the window, however, you can then use that name in TARGET attributes.


Previous Home Next
Windows and Frames Book Index The JavaScript Name Space

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell
This HTML Help has been published using the chm2web software.