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 how attributes work. It covers the target attribute, which chooses where a link opens, and the rel attribute, which describes the relationship between the current page and the linked page. The two are usually written together, so you need to know what each one does and what each one protects against.
What target does
By default, a link replaces the current page in the same browsing context (a tab, window, or frame). The target attribute changes that. Its value is the name of a browsing context, or one of four reserved keywords.
| Value | Behavior |
|---|---|
| `_self` | Opens in the current context. This is the default, so you rarely write it. |
| `_blank` | Opens in a new context, which is normally a new tab. |
| `_parent` | Opens in the parent of the current frame. Outside a frame it behaves like `_self`. |
| `_top` | Opens in the topmost context, which breaks out of all enclosing frames. |
_blank is the value you will use most. _parent and _top only matter when your page is displayed inside an iframe.
Any other value is treated as a context name. If a context with that name exists, the link loads there. If it does not, the browser creates one and gives it that name.
<a href="https://example.com/a" target="reference">First reference</a>
<a href="https://example.com/b" target="reference">Second reference</a>Both links share the name reference. The first click opens a new tab. The second click loads into that same tab instead of opening a third one. Named targets are useful for a reference panel, but they surprise users who expect each click to open a new tab. Use them only on purpose.
A basic new-tab link
<a href="https://example.com/" target="_blank" rel="noopener noreferrer">Example site (opens in a new tab)</a>target="_blank" opens the link in a new tab. rel="noopener noreferrer" limits what the new page can learn about, and do to, your page. The visible text "(opens in a new tab)" is deliberate, and a later section explains why.
Why rel matters: the opener problem
When a page opens a link with target="_blank", the new page receives a reference to the page that opened it, called window.opener. Without protection, a malicious or compromised destination page can run code such as window.opener.location = "https://evil.example/login". That silently redirects your original tab to a fake login page while the user is reading the new tab. This attack is called reverse tabnabbing.
The noopener keyword sets window.opener to null in the new page, so it cannot reach back. Two related effects also come with noopener. The new page may run in a separate process, which can improve performance. And the opener can no longer be navigated by the new page.
Current browsers treat target="_blank" as if rel="noopener" were present. That is the default behavior. You will still see the explicit keyword in a lot of code, for three reasons:
- Older browsers did not have the implicit behavior.
- The explicit keyword documents your intent.
- Linting tools and security reviewers often require it.
If you do want the new page to access window.opener, which is rare, you can opt in with rel="opener".
noreferrer and other rel values
When a browser follows a link, it normally sends the URL of the page you came from in the Referer request header. The noreferrer keyword suppresses that header. It also implies noopener, so rel="noreferrer" alone covers both. Writing both keywords is redundant but harmless.
Use noreferrer when the origin of the visit should not be disclosed, for example when your URLs contain private identifiers. It also makes your outbound traffic invisible to the destination's analytics. If you want a partner site to see where its visitors come from, leave noreferrer off. Using only noopener still sends the referrer.
The rel attribute accepts a space-separated list of keywords. Some keywords have nothing to do with security.
| Keyword | Meaning |
|---|---|
| `noopener` | The new page gets no `window.opener`. |
| `noreferrer` | Do not send the `Referer` header. Implies `noopener`. |
| `nofollow` | Hint to search engines that you do not endorse the target. |
| `ugc` | The link was added by users, such as a comment. |
| `sponsored` | The link is an advertisement or paid placement. |
| `external` | The target is a different site. This is informational. |
For links in user comments, rel="ugc nofollow noopener" is a common combination.
<a href="https://shop.example/offer" target="_blank" rel="sponsored noopener">Partner offer</a>rel is valid only on a, area, and link elements, and rel on a link that has no href does nothing.
Deciding when to use a new tab
Opening links in a new tab changes the user's browsing state. The Back button in the new tab has no history to return to, and some users do not notice that a new tab has appeared. This is especially disorienting for people who use screen readers or screen magnifiers. A good default is to leave links alone and let users choose, since they can always middle-click or use the context menu to open a tab themselves.
New tabs are reasonable in a few cases:
- The user is in the middle of a task that would be lost by leaving, such as filling in a long form that links to its terms of service.
- A link points to a reference document that the user will consult alongside the current page.
- A web application keeps state that cannot be restored by going back.
Telling users what will happen
WCAG, the web accessibility standard, discourages unexpected changes of context. A link that opens a new tab is a change of context, so say so in the link. You can write it in the visible text, as in the earlier example, or add a short icon with text for assistive technology.
<a href="https://example.com/terms" target="_blank" rel="noopener noreferrer">
Terms of service
<span class="visually-hidden">(opens in a new tab)</span>
</a>The visually-hidden class is not built in. You define it in CSS so the text stays available to screen readers but is not drawn on screen. Do not hide the warning with display: none, because that also removes it from the accessibility tree.
Common mistakes
- Putting
target="_blank"on every external link. Many sites do this by habit. It removes control from users and is not needed for security. - Forgetting
relon older code. Pages written before implicitnoopenermay still be viewed in older browsers. - Using
targeton same-page fragment links. A link like<a href="#install" target="_blank">opens a new tab at the same page, which is rarely intended. - Adding
targettomailto:ortel:links. It may flash an empty tab in some browsers. These links hand off to another application, so leavetargetoff. - Expecting
noreferrerto hide you from everyone. It affects only theRefererheader sent to the destination. The destination still sees your visitor's IP address. - Using a
targetname by accident. A typo such astarget="blank"without the underscore is a valid context name, so it opens one new tab and reuses it for later clicks.
Exercise
A page has the link <a href="https://partner.example/" target="_blank">Partner</a>. Name two changes you would make to improve it, and say what each change does.
Show answer
Add rel="noopener noreferrer" (or at least noopener). This stops the new page from using window.opener to redirect your tab, and noreferrer also stops the Referer header from being sent. Also change the link text to tell the user it opens in a new tab, for example "Partner (opens in a new tab)", so the change of context is not a surprise. If the link is a paid placement, add sponsored to rel as well.
Exercise
Two links both use target="docs" and point to different pages. The user clicks the first, then the second. How many tabs are open for these links, and why?
Show answer
One tab. The first click creates a browsing context named docs. The second click finds that context by name and loads its page there instead of opening another tab.