Sub topic 5
Opening links in a new tab: target and rel
This chapter assumes you can write an a element with an href, and that you know what an attribute is. It covers the target and rel attributes, which control where a link opens and what the new page can learn about the old one.
What target does
By default, a link replaces the current page. The target attribute names a browsing context where the link should open instead. A browsing context is a tab, a window, or a frame.
| Value | Behavior |
|---|---|
| `_self` | Opens in the current context. This is the default. |
| `_blank` | Opens in a new context, usually a new tab. |
| `_parent` | Opens in the parent frame. |
| `_top` | Opens in the top-level context, leaving any frames. |
In practice, you will use _blank almost every time you set target.
<a href="https://example.com/" target="_blank" rel="noopener noreferrer">Example site (opens in a new tab)</a>What rel does
The rel attribute describes the relationship between the current page and the linked resource. It takes one or more space-separated keywords. Two keywords matter for target="_blank":
noopenerstops the new page from reaching your page throughwindow.opener. Without it, a script on the other site could change your page, for example by redirecting it to a fake login page.noreferrerstops the browser from sending theRefererheader, which tells the destination which page the visitor came from. It also impliesnoopener.
Current browsers treat target="_blank" as if noopener were set. Writing it out is still useful. It documents your intent and protects visitors on older browsers.
The noreferrer keyword has a side effect. The destination site's analytics no longer see your page as a traffic source. If you want that data to flow, use rel="noopener" alone.
Common errors
- Adding
target="_blank"with norel. Modern browsers cover you, but older ones may not. - Opening every link in a new tab. Visitors can open a new tab themselves, and forcing one removes their choice. It also breaks the Back button, because the new tab has no history.
- Giving no warning. A new tab can confuse screen reader users and people who rely on the Back button. Say so in the link text or with a visible icon that has a text label.
- Using
rel="noreferrer"and expecting referral analytics to work.
<a href="https://example.com/report.html" target="_blank" rel="noopener">
Annual report (opens in a new tab)
</a>When to use it
Use a new tab when leaving the page would lose the visitor's work, such as a half-filled form, or when the link is a reference they will want beside the current page. For ordinary navigation, leave target off.
Exercise
A link has target="_blank" and rel="noreferrer". Does the new page still have access to window.opener, and what will the destination site's analytics show about where the visitor came from?
Show answer
No. The noreferrer keyword implies noopener, so window.opener is not available. The browser also omits the Referer header, so the destination site cannot see your page as the source of the visit.