Window Control refers to one of the most interesting features added to HTML - the possibility to control where the data appears when a user clicks on a link. This feature is most useful when used in conjunction with Frames.
Before the TARGET attribute, when a user clicked on a link, the new document either appeared in the same window or alternatively, and under user's control, it appeared in a new blank window. Now, targeting windows allows the document author to assign names to specific windows and target certain documents to appear in certain windows.
Presently, you can assign a name to a window in the following ways:
- By specifying the optional HTTP header in a document:
Window-target: window-name
This will force the document to load in the window named "window-name". If that window doesn't exist, one will be created.
- Via a targeted link. There's HTML code to assign a target "window-name" to a link. The document that the link refers to will load on that window.
- Using the NAME attribute of the FRAME tag to assign a name to a window created within a frameset. See Basic Frame Sintax.
Here's how to assign a name to a window using the TARGET attribute:
- In an A element:
This forces the link to load into the targeted window. If this window doesn't exists, it will be created and the document loaded into it.
<A HREF="url" TARGET="window-name">Target Window</A>
- This attribute can be added to a variety of other HTML tags - like AREA, BASE or FORM - to target the links referred to by that tag.
Window names, specified by TARGET attributes, must begin with an alpha-numeric character to be valid. Exceptions are the following "magic target names":
- TARGET="_blank"
This target will cause the link to always be loaded in a new blank not named window.
- TARGET="_self"
This target causes the link to always load in the same window the anchor was clicked in.
- TARGET="_top"
This target makes the link load in the full body of the window.
- TARGET="_parent"
This target makes the link load in the immediate FRAMESET parent of this document.
To have an idea of how these features work in conjunction with frames, consider the following example.
This document defines the frames used in a page:
<HTML>
<HEAD>
<TITLE>Window Control Example</TITLE>
</HEAD>
<FRAMESET COLS="50%,*">
<FRAME NAME="Frame1" SRC="doc1.html">
<FRAME NAME="Frame2" SRC="doc2.html">
</FRAMESET>
</HTML>
Now consider this code in doc2.html:
<HTML>
<HEAD>
<TITLE>Second Frame</TITLE>
</HEAD>
<BODY>
<A HREF="doc3.html">Target Example</A>
<A HREF="doc3.html" TARGET="Frame1">Target Example</A>
<A HREF="doc3.html" TARGET="New">Target Example</A>
<A HREF="doc3.html" TARGET="_top">Target Example</A>
</BODY>
</HTML>
The 4 links would act, respectively, as follows:
- The first one, having no TARGET attribute, would load the contents of doc3.html in the same window containing the link - "Frame2".
- The second one, having TARGET="Frame1", would load the contents of doc3.html in the window named "Frame1", which is the first frame in the frameset.
- The third one, with TARGET="New", would load the contents of doc3.html in the window named "New". As this window doesn't exist, a new window will be created with that name.
- Last, TARGET="_top", would make the contents of doc3.html load in the full body of the browser's window.
Another important subject to consider when using frames, is that not all the browsers available today support them. These browsers simply don't understand the FRAMESET and FRAME tags and, documents containing these elements, will not be correctly displayed.
To prevent this, HTML features a NOFRAMES tag. This element is ignored by frame-capable browsers and provides a simple way for you to create alternative content, viewable by non-frame-capable clients.
It's a good idea to include this element in every frame document, as in the following example:
<HTML>
<HEAD>
<TITLE>To All Browsers</TITLE>
</HEAD>
<FRAMESET COLS="300,*">
<FRAME NAME="Index" SRC="doc1.html">
<FRAME NAME="Content" SRC="doc2.html">
</FRAMESET>
<NOFRAMES>
<H1>ATTENTION!</H1>
<P>This document was designed using Frame Features.</P>
<P>You should consider upgrading your browser.</P>
</NOFRAMES>
</HTML>