5.4 The target Attribute
The target attribute on an anchor (<a>) or form (<form>) element specifies where to open the linked document. By default, browsers use _self, which replaces the current page. Common values:
_self– load in the same browsing context (default)_blank– open in a new tab or window_parent– load in parent frame (if inside an<iframe>)_top– load in full body of the window, breaking out of all frames- framename – a named
<iframe name='framename'>target
Example: Opening a link in a new tab:
Visit Example
If you use a custom frame name, name your iframe and reference it:
Chapter 1
5.4.1 Detailed Behaviors and Caveats
- _blank always creates a new browsing context. Modern browsers may reuse a hidden tab if links share the same source, but treat them as separate contexts.
- _self is implicit and safe, as it doesn’t spawn new windows or enable external manipulations.
-
Custom frame names must begin with a letter and cannot conflict with reserved names (
_self,_blank, etc.). -
When targeting cross-origin URLs, the
window.openerobject is set by default for_blanklinks, enabling potential reverse-tabnabbing attacks.
5.4.2 The rel Attribute
The rel attribute defines the relationship between the current document and the linked resource. Common values include:
| Value | Meaning |
|---|---|
noopener |
Prevents the new page from controlling window.opener (mitigates reverse-tabnabbing). |
noreferrer |
Omits the Referer header and also implies noopener in supporting browsers. |
nofollow |
Advises search engines not to follow the link for SEO ranking. |
external |
Indicates an external resource; purely semantic. |
Typical secure usage when combining target='_blank':
External Site
5.4.3 Security Best Practices
Using target='_blank' without further precautions exposes the page to reverse-tabnabbing. The newly opened page can call window.opener.location = 'malicious-site' to redirect your original page.
-
Always include
rel='noopener'when usingtarget='_blank'. -
Use
noreferrerif you also want to hide the referrer URL from the destination. -
Audit third-party links: ensure no untrusted scripts can exploit
window.opener. -
For enhanced isolation, consider using
<iframe sandbox>or Content Security Policy (CSP) directives.
Combined example with detailed comments:
rel='noopener noreferrer' > Safe External Link
5.4.4 Summary
-
targetcontrols where content loads; use it consciously to manage navigation flow. -
reldefines link relationships;noopenerandnoreferrermitigate security risks. -
Always pair
target='_blank'withrel='noopener noreferrer'to follow security best practices.
Leave a Reply
You must be logged in to post a comment.