spacer

Creating Hyperlinks in Frames

 
Creating hyperlinks in HTML frames is a little more complicated than creating them in standard web pages. The key difference is outlined in the following:
  • In a standard web page, clicking on a hyperlink will —by default— reload the original browser window with the new page.
  • In a framed web page, on the other hand, clicking on a hyperlink will —by default— reload the frame in which the hyperlink is located in with the new page.

Loading the new page in the same frame, however, may only be desirable in certain situations.

In other situations, you may instead require that:
  1. A hyperlink in one frame loads a new page in another frame.

    OR
  2. A hyperlink loads a new page which breaks out of the frame.

In all cases, to make sure hyperlinks in a framed page perform their intended function, you will have to do two things:
  1. Use the name attribute to give each of your frames a special identity.
  2. Use the target attribute in your hyperlinks.
There are also some standard frame target names predefined by the HTML specification which control other possibilities of handling hyperlinks in frames. All of this is detailed below starting with how...


A Hyperlink in One Frame Loads a New Page in Another Frame


In the following example, we'll refer to our basic frame layout, (discussed in more detail on the previous page) where we created two framed columns. These two columns are specified by the two <frame> tags in the frameset document.


THE name ATTRIBUTE

The name attribute is used in each <frame> tag to allow you to give that frame a special identity. This identity will serve as a 'target name' which is used in hyperlinks that load new pages intended for that frame.

The value of the name attribute can be anything you want as long as it's unique. In the source code of our frameset document, the menu frame is simply called 'menu' and the content frame is called 'content':

SOURCE CODE - name attributes highlighted

<html>

<head>
<title>HTML Frames - A Basic Frame Layout</title>
</head>

<frameset cols="25%,75%">
<frame src="menu.htm" name="menu">
<frame src="chapter1.htm" name="content">
</frameset>

</html>




THE target ATTRIBUTE

Now that we've given each frame a special identity, let's create hyperlinks in the menu frame that load new pages into the content frame. To do this, we simply use the standard HTML hyperlink code and then insert the target attribute. The value of the target attribute is the name of the frame (in this case "content") into which you wish to load the new web page.

The code of menu.htm, the frame source document of the menu frame, illustrates the use of the target attribute:

SOURCE CODE - menu.htm uses target attributes in hyperlinks

<html>

<head>
<title>Site Menu</title>

</head>

<body>

<center><h3>War of the Worlds</h3>
<b><i>by H. G. Wells</i></b><br>
<br>

<a href="chapter1.htm" target="content">Chapter 1</a><br>
<a href="chapter2.htm" target="content">Chapter 2</a><br>
<a href="chapter3.htm" target="content">Chapter 3</a><br>
<a href="chapter4.htm" target="content">Chapter 4</a><br>

</body>

</html>




DEFINING A DEFAULT TARGET

If the hyperlinks in a particular frame are always intended to load a new page in another frame then there is a shortcut you can use to save yourself having to insert the target attribute in all your links. All you have to do is insert the base element in between the <head>...</head> tags of the frame source document containing the links. The target attribute is then used in the base element to define the default target for any hyperlink on that web page. Thus, in our basic frame layout, the source code of menu.htm could be rewritten as follows:

SOURCE CODE - menu.htm uses base element to define default target

<html>

<head>
<title>Site Menu</title>

<base target="content">

</head>

<body>

<center><h3>War of the Worlds</h3>
<b><i>by H. G. Wells</i></b><br>
<br>

<a href="chapter1.htm">Chapter 1</a><br>
<a href="chapter2.htm">Chapter 2</a><br>
<a href="chapter3.htm">Chapter 3</a><br>
<a href="chapter4.htm">Chapter 4</a><br>

</body>

</html>



In the absence of the base element, all hyperlinks in framed pages should contain the target attribute. This is especially important when creating hyperlinks to web pages that are not on your web site, a.k.a., external links. If you neglect to specify a target in the hyperlink code, the new page will —by default— load into the frame that the hyperlink is located in. This will result in someone else's content displayed in your frame giving the appearance that their content is part of your website. To avoid this —and any legal hassles that may arise as a result of this— you'll need to understand how...


A Hyperlink Loads a New Page Which Breaks Out of the Frame


The target="_top" attribute/value pair is used to create a hyperlink that —when activated— loads a new page which breaks out of all framesets. The new page is thus loaded into the original full browser window.

Here's an example of the source code used in this scenario:


<a href="http://example.com/" target="_top">Example.com</a>


(Example.com represents the website you wish to load outside of your frameset and into the full original browser window.)


Recognized Frame Target Names


A set of frame target names are specially reserved by the HTML specification. These target names are recognized by browsers to accomodate a number of different scenarios when creating hyperlinks in frames. They are used as the value of the target attribute in your hyperlink code to perform their respective functions. Note that they are all preceded by the underscore ( _ ) character.

Here's the syntax of the hyperlink code:


<a href="web address" target="frame target name">link text</a>


Their functions are outlined in the table below:


Frame target nameWhat it does
_top Loads the new page into the full original browser window (see above).
_blank Loads the new page into a completely new browser window. (You would thus have two browser windows open: one containing the original framed page and the other containing the new page.)
_self Loads the new page into the same frame in which the hyperlink exists in. (If you don't specify a target in your hyperlink then this is what happens by default.)
_parent Loads the new page into the immediate frameset parent of the frame in which the hyperlink exists in. (Useful in advanced frame layouts.)


*   *   *


Alright then, let's continue on to learn how to tweak those frames so that they don't look so butt ugly...