Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to make a link open in a new tab in HTML

Understanding the Basics

When venturing into the world of web development, there’s a lot to learn. But a concept that’s simple yet incredibly useful is making a link open in a new tab using HTML. It’s like opening a new door, while keeping the old one intact, allowing you to easily go back to where you started.

Imagine you're reading an interesting article online, and you come across a link to another article. You want to read this second article, but you don't want to navigate away from the current one. This is where opening a link in a new tab comes in. When you click the link, the new article opens in a separate tab in your browser, leaving the original article untouched in its own tab.

The magic attribute: target="_blank"

In HTML, we can control how links behave using attributes. One such attribute is target. This attribute determines where the linked resource will open. When the target attribute is set to _blank, the link will open in a new tab (or window, depending on your browser settings).

Here's an example of what that looks like in code:

<a href="" target="_blank">Click me!</a>

This line of code creates a hyperlink that says "Click me!". When you click on this hyperlink, the URL will open in a new tab.

Breaking down the code

Let's look closer at the code.

The <a> tag in HTML is used to create hyperlinks. The word "a" is short for "anchor". You can think of it as an anchor that ties a piece of text to a URL.

The href attribute stands for "Hypertext REFerence". It specifies the URL of the page the link goes to. It's like the address written on a letter envelope.

The target attribute specifies where to open the linked document. The _blank value tells the browser to open the linked document in a new tab or window.

Security concern: The rel="noopener" attribute

While the target="_blank" attribute is quite useful, it does come with a security risk. The new page has access to your page via the window.opener property, which could lead to some security vulnerabilities.

To mitigate this risk, when using target="_blank", it's recommended to always add rel="noopener" to your anchor tags. This attribute prevents the new page from being able to access the window.opener property.

Here's what that looks like in code:

<a href="" target="_blank" rel="noopener">Click me!</a>

With the rel="noopener" attribute added, the new tab can't access your page via the window.opener property, keeping your page more secure.

In conclusion

Learning to make a link open in a new tab in HTML is like learning to open a new door without closing the old one. It's a simple yet useful skill that enhances the user experience by allowing users to navigate your site without losing their place. Just remember to always use the rel="noopener" attribute with target="_blank" to keep your site secure.

As you continue your journey in programming, remember that every new concept you learn is another tool in your toolbox. The more tools you have, the more you can do. So keep learning and keep building!