Sign in
Let users explore external content without losing their place — a tiny tweak for a smoother journey.
Want to give your website visitors a seamless browsing experience in web navigation? By learning how to open links in new tabs, you can ensure that users can explore external content without leaving your site.
This article will cover how to open a link in a new tab, the associated security implications, and best practices to follow.
Let’s dive in!
In HTML, links are created using the anchor tag < a>
. The href attribute specifies the link address, and its value is the URL of the page the link goes to. The browser opens the page in the same tab by default when the link is clicked or pressed.
1< a href="https://example.com">Visit Example.com< /a>
To open the link in a new tab, add the target attribute and set its value to "_blank"
(the blank keyword) to the anchor tag. Depending on the browser's settings, this tells the browser to open the linked document in a new tab or window (sometimes referred to as a new window).
1< a href="https://example.com" target="_blank">Visit Example.com< /a>
Here, we set the target attribute to "_blank"
to ensure the link opens in a new tab or new window.
Using target=”_blank”
introduces a security vulnerability known as “tabnabbing” when a linked URL is used. This occurs when the newly opened tab can manipulate the original tab through the window.opener property. What is happening here is that the new tab can manipulate the original tab, potentially redirecting users to a malicious page.
This can lead to phishing attacks where users may unknowingly enter their login details on a fake page. This can result in a redirect to a fake login page, tricking users into entering their credentials. If users are already logged in, this exploit can compromise their accounts. The linked page can gain partial access to the linking page through the window.opener
object, which can be exploited for such attacks. To prevent this, always include rel=”noopener noreferrer”
with target=”_blank”
.
1< a href="https://example.com" target="_blank" rel="noopener noreferrer">Visit Example.com< /a>
Place this anchor tag within the body of your HTML document, not in the head.
The rel
attribute with noopener and noreferrer will omit the window.opener
reference, preventing the new page from accessing the original tab. The noopener keyword ensures that the new page cannot access the window.opener
property, while noreferrer prevents the browser from sending the referrer header to the new page.
Great user navigation starts with details like this, but is built on a solid project setup. Spend more time perfecting your user experience.
Links don’t just have to be text. You can use images within anchor tags to create clickable images that open in a new tab.
1<a href="https://example.com" target="_blank" rel="noopener noreferrer"> 2 <img src="example.jpg" alt="Example Image"> 3</a>
When adding social media links to your website, it’s a good practice to open these in a new tab so users don’t leave your site entirely.
1<ul> 2 <li><a href="https://twitter.com/example" target="_blank" rel="noopener noreferrer">Twitter</a></li> 3 <li><a href="https://facebook.com/example" target="_blank" rel="noopener noreferrer">Facebook</a></li> 4</ul>
Sometimes, you might need to use JavaScript code to dynamically add the target="_blank"
attribute to links.
1< script> 2document.querySelectorAll('a').forEach(link => { 3 link.setAttribute('target', '_blank'); 4 link.setAttribute('rel', 'noopener noreferrer'); 5}); 6< /script>
This solution allows you to ensure all links open in a new tab programmatically.
This script will add target="_blank"
and rel="noopener noreferrer"
to all anchor tags on the page. You can also modify specific links by targeting them with JavaScript.
In React applications, you can implement similar logic to control how links open.
Opening links in a new tab can be beneficial in scenarios where users need to refer back to the original page. For example, when filling out a form or reading a lengthy article, opening a link in the same tab might disrupt the workflow. The hope is to improve the user experience by allowing users to keep their place on the original page.
Note: Overusing this feature can lead to a cluttered browsing experience.
Opening links in a new tab can enhance your website's usability significantly. However, following SEO best practices is essential to ensure you don’t inadvertently harm your site’s search engine rankings or user experience. Here are the key considerations and best practices:
1< a href="https://example.com" target="_blank">Visit Example< /a>
target=“_blank”
judiciously, primarily for external links or links that might disrupt the user’s current session.Preventing Security Issues: Adding rel=“noopener noreferrer” is crucial when using target=“_blank”
. This prevents the new page from being able to access the window.opener property, which can protect against certain types of phishing attacks (known as tabnabbing).
Syntax Example:
1<a href="https://example.com" target="_blank" rel="noopener noreferrer">Visit Example</a>
target=“_blank”
could lead to a poor user experience, indirectly affecting bounce rates and session durations, which are SEO factors.1<a href="https://example.com" target="_blank" rel="noopener noreferrer">Visit Example <span class="sr-only">(opens in a new tab)</span></a>
1const link = document.createElement('a'); 2link.href = 'https://example.com'; 3link.target = '_blank'; 4link.rel = 'noopener noreferrer'; 5document.body.appendChild(link); 6link.click(); 7document.body.removeChild(link);
Here, the link element is temporarily added to the document's body to trigger the click event.
Using HTML to open links in a new tab enhances user experience. Always balance usability, security, and SEO considerations to ensure your site's user-friendly and search-engine-friendly. By following the guidelines outlined in this guide, you can effortlessly create a smooth and intuitive browsing experience for your visitors.