target=_blank, rel=noopener noreferrer & origins

Links that leave your site

Lesson 7 of 20 · 10 min · Module 2: Safe HTML and pages

A link that opens a new tab used to give the new page a handle on yours, and every link can tell the next site where the visitor came from. A few attributes close both gaps and make the behaviour clear to everyone.

Start this lesson →

Free. No installation: you write and run the code in your browser, and it is checked automatically.

By the end, you’ll be able to…

  • Explain reverse tabnabbing and referrer leaks
  • Tell your own links from other sites by comparing origins
  • Mark external links with target, rel and an accessible hint

The idea

When a link opens with target="_blank", the new page could receive window.opener and send your tab to a fake login page; this trick is called reverse tabnabbing. rel="noopener" removes the handle, and noreferrer also stops the browser from sending your page’s address. Recent browsers apply noopener to such links by default, but setting it explicitly protects older ones and documents your intent.

const link = document.querySelector('a');
link.target = '_blank';
link.relList.add('noopener', 'noreferrer');

01What a new tab could do

Without noopener, a page opened from your link gets window.opener and can quietly send your original tab to another address, such as a copy of your login page. People returning to that tab trust it, because it was yours a moment ago.

02Compare origins, not text

An origin is the scheme, host and port together. new URL(href, site) resolves relative links against your site, so /guides/xss becomes https://learn.example/guides/xss and compares correctly, while learn.example.evil.com does not match.

console.log(new URL('/faq', 'https://learn.example').origin);
Output
https://learn.example

03Say what will happen

A new tab takes people out of their context and breaks the Back button. Tell them in the link itself, with visible text, an icon plus hidden text, or both, and open new tabs only when it really helps.

Your turn

Write markExternalLinks(container, site). For every link with an href inside container whose address, resolved with new URL(href, site), uses http: or https: and has a different origin than site: set target="_blank", add noopener and noreferrer to its rel without removing existing values, add the class external, and append a <span class="visually-hidden"> with the text (opens in a new tab). Leave other links alone, skip links that are already marked, and ignore addresses that cannot be parsed.

What the checks look for

  • External links open safely in a new tab
  • Existing rel values are kept
  • Screen readers hear that a new tab opens
  • Links within the site and email links stay as they are
  • Links added later can be marked too
  • Marking twice changes nothing
  • Broken addresses do not stop the script
Try it in the editor →

Take this with you

External links get noopener noreferrer, and people learn that a new tab opens.

Go a little further

Add <meta name="referrer" content="strict-origin-when-cross-origin"> to the page and find out what it sends to other sites.

Learn Web Security Foundations for free

20 short lessons with real code and instant checks.

See the whole course →