Target a Window or Frame Using JavaScript or HTML

Learn to use top.location.href and other link targets in Java

As you almost certainly know, windows and frames are terms used to describe what may appear when you click on a link in a website. Without extra coding, links will open in the same window you're currently using, meaning you'll need to hit the "Back" button to return to the page you had been browsing.

But if the link is defined (coded) to open in a new window, it will appear in a new window or tab on your browser.

If the link is defined (coded) to open in a new frame, it will pop up on top of the current page in your browser.

With an ordinary HTML link using the <a> tag, you can target the page the link refers to in a way that the link, when clicked, will display in another window or frame. Of course, the same can also be done from within Javascript—in fact, there's plenty of overlap between HTML and Java. Generally speaking, you can use Java to target most types of links.

Using top.location.href and Other Link Targets in Java

Here are ways you can code in both HTML and JavaScript in order to target links so that they open either in new blank windows, in parent frames, in frames within the current page, or in a specific frame within a frameset.

For example, as described in the following chart, to target the top of the current page and break out of any frameset currently in use you would use <a href="page.htm" target="_top"> in HTML.

In Javascript you use top.location.href = 'page.htm';, which achieves the same objective.

Other Java coding follows a similar pattern:

Link EffectHTMLJavaScript
Target a new blank window<a href="page.htm" target="_blank">"_blank");
Target top of the page<a href="page.htm" target="_top">top.location.href = 'page.htm';
Target current page or frame<a href="page.htm" target="_self">self.location.href = 'page.htm';
Target parent frame<a href="page.htm" target="_parent">parent.location.href = 'page.htm';
Target a specific frame within a frameset<a href="page.htm" target="thatframe">top.frames['thatframe'].location.href = 'page.htm';
Target a specific iframe within current page<a href="page.htm" target="thatframe">self.frames['thatframe'].location.href = 'page.htm';

Note: When targeting a specific frame within a frameset or targeting a specific iframe within the current page, replace "thatframe" shown in the code with the name of the frame where you want the content to be displayed. However, make sure to keep the quotation marks—they're important and necessary.

When using JavaScript coding for links, you should use it in conjunction with an action, such as onClick, or onMousover. This language will define when the link should be opened.