HTML <a> target attribute
Definition and usage
target
The attribute specifies the location where the linked document is to be opened.
Example
The target attribute specifies the location where the linked document is to be opened:
<a href="https://www.codew3c.com" target="_blank">Visit CodeW3C.com</a>
Syntax
<a target="_blank|_self|_parent|_top|framename">
target
The purpose of this attribute is to inform the browser where you want to display the linked resource. By default, the browser uses the window, tab, or frame (iframe) that displays the current document, so the new document will replace the currently displayed document. However, there are other options, please see the table below:
Attribute value
Value | Description |
---|---|
_blank | Open the linked document in a new window or tab. |
_self | Open the linked document in the same frame as the click (default). |
_parent | Open the linked document in the parent frame. |
_top | Open the linked document across the entire body of the window. |
framename | Open the linked document in the specified iframe. |
Detailed explanation
If a target attribute is included within an <a> tag, the browser will load and display the document named by the href attribute of this tag, which matches the name of the frame or window. If the specified name or id of the frame or window does not exist, the browser will open a new window, assign a specified identifier to this window, and then load the new document into that window. From then on, the hyperlinked document can point to this new window.
Open in a new window
Hyperlinks that point to a destination make it easy to create efficient browsing tools. For example, a simple list of content documents can redirect the documents to a separate window:
<h3>Table of Contents</h3> <ul> <li><a href="pref.html" target="view_window">Preface</a></li> <li><a href="chap1.html" target="view_window">Chapter 1</a></li> <li><a href="chap2.html" target="view_window">Chapter 2</a></li> <li><a href="chap3.html" target="view_window">Chapter 3</a></li> </ul>
When a user first selects a link from the content list, the browser will open a new window, marking it as "view_window", and then display the content of the document that is to be displayed. If the user selects another link from this content list and the "view_window" is still open, the browser will again load the selected document into that window, replacing the previous documents.
Throughout the process, the window that contains the content list is accessible to the user. By clicking on a link in the window, the content of another window can be changed.
Open a window in the frame
Instead of opening a full browser window, the more common method of using target is to direct hyperlinks in a <frameset> display to one or more frames. This content list can be placed in one of the frames in a document with two frames, and the adjacent frame can be used to display the selected document:
<frameset cols="100,*"> <frame src="toc.html"> <frame src="pref.html" name="view_frame"> </frameset>
When the browser initially displays these two frames, the left frame contains the directory, and the right frame contains the preface.
This is the source code of "toc.html":
<h3>Table of Contents</h3> <ul> <li><a href="pref.html" target="view_frame">Preface</a></li> <li><a href="chap1.html" target="view_frame">Chapter 1</a></li> <li><a href="chap2.html" target="view_frame">Chapter 2</a></li> <li><a href="chap3.html" target="view_frame">Chapter 3</a></li> </ul>
Please note that in the document "toc.html", each link targets "view_frame", which is the frame on the right.
When a user selects a link from the directory in the left frame, the browser will load and display the associated document in this "view_frame" frame on the right. When another link is selected, the content in this frame on the right will also change, while the left frame remains unchanged.
Special target
There are 4 reserved target names used for special document redirection operations:
_blank
Browsers always load the target document in a new, unnamed window.
_self
This target value is the default target for all <a> tags that do not specify a target, it makes the target document load and display in the same frame or window as the source document. This target is redundant and unnecessary unless used with the target attribute of the <base> tag in the document title.
_parent
This target makes the document load in the parent window or in the frameset that contains the frame referenced by the hyperlink. If the reference is in a window or in the top-level frame, it is equivalent to the target _self.
_top
This target makes the document load in the window containing this hyperlink, and using the _top target will clear all frames included and load the document in the entire browser window.
Tip:All 4 values of target are preceded by an underscore. Any other window or target that starts with an underscore will be ignored by the browser, so do not use an underscore as the first character of any frame name or id defined in the document.
Browser Support
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
Support | Support | Support | Support | Support |