advertisement -- please support sponsors

frames, navigation, windows:
targeting the first window

What is the target name of the first Netscape browser window?

Consider the following hyperlink definition:
<a href="next.html" target="other_window">click here</a>
When a user clicks on this hyperlink, the document next.html will be loaded into the window or frame named "other_window." If there is no window or frame already existing by that name, a new window with that name will automatically be created.

The ability to create, name, and target windows is a side-effect of the frames extensions promoted by Netscape in Navigator 2.0. Many web site builders use this feature to create hyperlinks that load documents in "other" browser windows.

Creating new windows and targeting them in hyperlinks is easy, because we know the new windows' names. Targeting the original window, however, is more difficult, since its name is unknown. If we just knew what the first window's name was, then perhaps we could use it in the target fields of hyperlinks in other windows, right?

Wrong. The unfortunate truth is that the first window doesn't have a name. This doesn't mean it can't be targeted, though, since you can still use the reserved target name "_parent", right?

Wrong again. The reserved target name _parent refers to the parent frame of the frame containing the hyperlink, not another window. If the parent frame doesn't exist, then _parent is synonymous to _self. The reserved frame name _top has similar limitations. Unfortunately, none of the special frame names can be used to target other windows.

Now that we've talked about what we can't do, let's focus on what we can do. How do you target the browser's first window in a hyperlink? The answer may surprise you. Just give the first window a name! It's that simple.

Just because it's already created doesn't mean you can't still give it a name. JavaScript makes it possible. All you need to do is determine which page on your web site is most likely to be loaded into the original browser window first (for example, your home page), and then insert the following lines into the source:

<script> self . name = "first_window"; </script>
When the document is loaded, the script will automatically be executed, and the browser window containing the document will be assigned the target name "first_window." You can then use "first_window" as a target name in hyperlinks. (If you don't like the name "first_window," feel free to make it whatever you like. Perhaps something like "_original" would be better.)

Once a window has been given a name in the manner, the name will stay with the window until the window is closed or the name is changed by another script.* Thus, there is no need to include the script in every document that is loaded into the original window. Just put it in the starting page, and all should go well.

* Here is an interesting afterthought: Because a window's target name remains intact even when new documents are loaded, the window object's name property might be a convenient place to store information that needs to be passed on from one document's script to the next.

Charlton Rose
8 Jan. 1997