Frames
Naming Frames and Targeting Links
Simple Examples |
No Frames |
Borders and Borderless Frames |
Nested Framesets |
Frame Margins |
Naming Frames & Targeting Links |
Frames Exercise
Another often useful attribute that we can include in the
frame tag is NAME. Aptly named,
this attribute allows us to name a frame. We'll see in in
a moment how the name can be used. Here's an example of
naming frames:
<frameset rows="20%,80%">
<frame src="file1.html" name="frame1">
<frame src="file2.html" name="frame2">
</frameset>
|
Here we've given our two frames the (not very imaginative)
names frame1 and frame2. We can name our frames just about
anything we want, as long as use only alphanumeric characters
in forming the names. There are a few "reserved" frame names
that have special meanings; we'll look at these names later.
Once a frame has been named, it can be "targeted". That
is, links in other frames can be instructed to open files
in a named frame, rather than obeying their default behavior
(which generally means opening in the frame where the link
is located).
Perhaps this targeting behavior sounds a bit familiar?
If you've already looked at this tutorial's discussion of
targeting links to open in new
browser windows (or if you've already had experience
doing this), you may now be getting a feeling of deja vu
all over again.
In the discussion of opening links in other windows (either
new or pre-existing, but not the window containing the link),
we see that it is possible to target a link in one window
to open a file in another window. We can do this with HTML
by adding the TARGET attribute to a link; for example,
this link opens in the current
window. The link uses the standard sort of <a> tag
that we've had lots of experience with by now:
<a href="linked.html">link</a>
but this link
opens the file named linked.html in a new window that we
have named (again unimaginatively) newwin. This latter link
was coded as
<a href="linked.html"
target="newwin">link</a>
In our discussion of opening files in other windows we see
that we can also use JavaScript to accomplish the targeting.
As far as targeting links is concerned, frames are treated
the same as windows. Just as we can target a link to open
in a named window either by adding the TARGET attribute
to an HTML <a> tag, or by using the JavaScript window.open
function, we can also use either approach to open a linked
file in a named frame. In both cases, if the named window
or frame is already open, the linked file will be loaded
into the window or frame when the link is clicked. If no
window or frame with the specified name is already open,
a window with that name will be created and the linked
file opened in it when the link is clicked. (Frame sets
cannot be spontaneously created by opening new links. They
can only be created in the way we described earlier in this
chapter. From the earlier discussion is should be obvious
why this is true.)
A Simple Example of
Targeting Links
As we mentioned earlier, one of the most common uses of
frames is to create a set of two frames, with one serving
as a table of contents that is always displayed. This frame
has links to material that gets displayed in the other frame
of the set. For the sake of future reference, let's call
these the toc frame (for "table of contents") and display
frame, respectively. You learned earlier how to create such
a simple frame set. And now you know how links in the toc
frame can be targeted into the display frame.
To implement a very simple example of such a frame set
let's create an enhancement of Example 2. We start with
the set of named frames that we looked at in the previous
section (which is Example 2 modified to use named frames).
Now we make an additional modification to the frame set:
instead of loading file2.html into the second frame, we
load a file there that contains a collection of targeted
links. The new frame set is now:
<frameset rows="20%,80%">
<frame src="file1.html" name="frame1">
<frame src="toc.html"
name="frame2">
</frameset>
|
The file with our list of targeted links is named toc.html;
it contains:
<html>
<head>
<title>Table of Contents</title>
</head>
<body bgcolor="#ffffff">
Table of Contents
<p>
<a href="nfile1.html" target="frame1">nfile1</a>
<a href="nfile2.html" target="frame1">nfile2</a>
<a href="nfile3.html" target="frame1">nfile3</a>
</body>
</html>
|
toc.html
|
|
Here is the result: Example
12. By clicking on a link in the toc frame we load a
new file into the display frame.
Special Reserved Names
for Link Targets
While you can generally name a frame or window about anything
you wish, there are four reserved names that have special
significance when used as targets of links: _blank,
_self, _parent, and _top. (Note that
each of these names begins with the underscore character.)
- When _blank is specified
as the target name, the link opens in a new unnamed window.
- A link targeted to _self
will open in the same frame or window as the link. (This
will override other HTML features that could establish
a different default.)
- A link targeted to _parent
opens in the frame or set that encloses or created the
frame with the link: Example
13.
- If a link is targeted to
_top, it will open in the full browser window,
breaking out of the frame set (in fact, out of all nested
frame sets) containing the
link: Example
14.
Note that since _blank opens a file in a new unnamed
window, that window can't be targeted for other links. So
if you try to target several links with _blank, each
will open in a separate new window.
|