Targeting Links in HTML IFrames and Frames

Decide where your links should open

HTML code for building a web form
Image courtesy Gary Conner / Photolibrary / Getty Images

The iframe tag is used to display a web page inside a web page. When you create a document to be inside an iframe, any links in that frame will automatically open in that same frame. But with the attribute on the link (the element or elements), you can specify where the links will open.

The first step is to give your iframe a unique name with the name attribute. Then, it's a matter of pointing your links at that frame using the ID as the value of the target attribute:

<iframe src="example.htm" name="page"></iframe>
<a href="https://www.example.com" target="page">Example</a>

If you add a target to an ID that doesn't exist in the current browser session, the link will open in a new browser window, with that name. After the first time, any links that point to that named target will open in the same new window.

If you don't want to name every window or every frame with an ID, you can still target some specific windows without needing a named window or frame. These are called the standard targets.

The Four Target Keywords

There are four target keywords that don't require a named frame. These keywords allow you to open links in specific areas of the web browser window that might not have an ID associated with them. These are the targets that web browsers recognize:

_self

This is the default target for any anchor tag. If you don't set the target attribute or you use this target, the link will open in the same window or frame that the link is in.

_parent

Iframes are embedded inside web pages. You can embed an iframe in a page that is inside another iframe on another web page. When you set the target attribute to _parent, the link will open in the web page that is holding the iframe.

_top

In most situations with iframes, this target will open links in the same way that the _parent target does. But if there is an iframe inside an iframe, the _top target opens links in the highest-level window in the series, removing all the iframes.

_blank

The most commonly used target, this opens the link in an entirely new window, similar to a popup.

How to Name Your Frames

When you build a web page with iframes, it's a good idea to give each one a specific name. This helps you remember what they are for and allows you to send links to those specific frames. For example:

name="stats"
name="external-document"

Setting a Default Target

You can also set a default target on your web pages using the element. Set the target attribute to the name of the iframe you want all links to open in. You can also set default targets for one of the four target keywords.