Target a Window or Frame Using JavaScript or HTML

Use top.location.href and other link targets in Java

Web browser
Adam Gault/OJO Images/Getty Images

Windows and frames are terms used to describe what may appear when you click a link in a website. Without extra coding, links will open in the same window you're currently using, meaning you'll need to press the back button to return to the page you had been browsing.

But if the link is defined to open in a new window, it will appear in a new window or tab on your browser. If the link is defined 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 anchor 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

Code in either HTML or JavaScript 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, 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 Effect HTML JavaScript
Target a new blank window <a href="page.htm" target="_blank"> window.open("_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';

When you target a specific frame within a frameset or 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, keep the quotation marks — they're necessary.

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

Format
mla apa chicago
Your Citation
Chapman, Stephen. "Target a Window or Frame Using JavaScript or HTML." ThoughtCo, Aug. 25, 2020, thoughtco.com/target-a-window-or-frame-using-javascript-or-html-4092194. Chapman, Stephen. (2020, August 25). Target a Window or Frame Using JavaScript or HTML. Retrieved from https://www.thoughtco.com/target-a-window-or-frame-using-javascript-or-html-4092194 Chapman, Stephen. "Target a Window or Frame Using JavaScript or HTML." ThoughtCo. https://www.thoughtco.com/target-a-window-or-frame-using-javascript-or-html-4092194 (accessed March 28, 2024).