Always use noopener or noreferrer for links opened in new tabs
Written byPhuoc Nguyen
Created
16 Mar, 2021
Category
Practice
Tags
HTML
In order to open a link in a new tab, we use the
`target="_blank"`
attribute. However, it can lead to some issues if you aren't aware of them.First, the newly opened tab uses the same process with the opener one. Hence, it can slow down your page.
More importantly, the new tab is able to access the
`window`
object of the opener page via the `window.opener`
object. Imagine that the new tab executes the following code:js
window.opener.location = 'http://fake.website.here';
As the code implies, it redirects the original tab to a fake website. What happens if the fake website has the same UI as the real one? Since users already opened it, they may not realize that the website they are looking at isn't real.
Adding
`rel="noopener"`
fixes the issues.It's good to know that there is the
`rel="noreferrer"`
attribute. It not only fixes the issues that `noopener`
does, but also prevents the `Referer`
header from being sent to the new tab.html
<!-- Don't -->
<a target="_blank">...</a>
<!-- Do -->
<a target="_blank" rel="noopener">...</a>
<!-- Or -->
<a target="_blank" rel="noreferrer">...</a>
<a target="_blank" rel="noopener noreferrer">...</a>
Some modern browsers, such as Chrome 88+, automatically adds the`noopener`
behavior if it's missing. However, it's still recommended to add`rel="noopener"`
or`rel="noreferrer"`
to avoid the security and performance issues in old legacy browsers.
Questions? 🙋
Do you have any questions about front-end development? If so, feel free to create a new issue on GitHub using the button below. I'm happy to help with any topic you'd like to learn more about, even beyond what's covered in this post.
While I have a long list of upcoming topics, I'm always eager to prioritize your questions and ideas for future content. Let's learn and grow together! Sharing knowledge is the best way to elevate ourselves 🥷.
Recent posts ⚡
Newsletter 🔔
If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn