Table of contents
1.
Introduction
2.
Approach to Open Link in a New Tab
2.1.
Why Use target="_blank"?
3.
Syntax
4.
Examples
4.1.
Basic Example
4.2.
Example with Additional Attributes
5.
Advanced Examples and Use Cases
5.1.
Opening a Link in a New Tab with JavaScript
5.2.
Opening Multiple Links in New Tabs
5.2.1.
Use Case
5.3.
Open Link in a New Tab with Styled Button
6.
Using JavaScript to Open Links in a New Tab  
6.1.
How It Works  
6.2.
Why Use JavaScript?  
6.3.
Why and When to Open Links in a New Tab  
6.4.
Why Use This Feature?  
6.5.
When Should You Use It?  
7.
Example: Opening External Links in a New Tab  
7.1.
Avoid Overusing This Feature  
7.2.
User Experience Considerations  
8.
Frequently Asked Questions
8.1.
Why should I use rel="noopener noreferrer" when using target="_blank"?
8.2.
Can I make an image link open in a new tab?
8.3.
How do I force all links to open in a new tab?
9.
Conclusion
Last Updated: Feb 23, 2025
Medium

How to Open Link in a New Tab in HTML?

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Opening a link in a new tab in HTML is a simple yet essential feature for improving user navigation. By using the target attribute with the value "_blank", you can ensure that clicking a link opens a new tab instead of replacing the current page. 

How to Open Link in a New Tab in HTML?

In this article, you will learn about the target attribute, its usage, and best practices for implementing it in your HTML documents.

Approach to Open Link in a New Tab

The standard way to open a link in a new tab in HTML is by using the target="_blank" attribute inside the <a> tag. The target attribute defines where the linked document will open when the user clicks the hyperlink.

Why Use target="_blank"?

  • User Experience: It allows users to view the linked page without losing their current page.
     
  • Navigation Control: Helps keep important content accessible.
     
  • Multi-tasking: Useful when referencing external resources while staying on the same page.

Syntax

The basic syntax for opening a link in a new tab in HTML is:

<a href="URL" target="_blank">Link Text</a>

 

Explanation:

  • href="URL" specifies the destination URL.
     
  • target="_blank" instructs the browser to open the link in a new tab.

Examples

Basic Example

The following example demonstrates how to open Google in a new tab when a user clicks on the link.

<a href="https://www.google.com" target="_blank">Open Google</a>

 

Output:

Clicking on "Open Google" will open Google in a new tab.

Example with Additional Attributes

<a href="https://www.codingninjas.com" target="_blank" rel="noopener noreferrer">Visit Coding Ninjas</a>

 

Explanation:

  • rel="noopener noreferrer" enhances security by preventing the new page from accessing the original page’s window.opener property.

Advanced Examples and Use Cases

Opening a Link in a New Tab with JavaScript

If you want more control over link behavior, you can use JavaScript.

<button onclick="openNewTab()">Open in New Tab</button>
<script>
  function openNewTab() {
    window.open("https://www.codingninjas.com", "_blank");
  }
</script>

 

Explanation:

  • The window.open() function opens a specified URL in a new tab when the button is clicked.

Opening Multiple Links in New Tabs

You can use JavaScript to open multiple links in separate tabs simultaneously.

<button onclick="openMultipleTabs()">Open Multiple Links</button>
<script>
  function openMultipleTabs() {
    window.open("https://www.google.com", "_blank");
    window.open("https://www.codingninjas.com", "_blank");
  }
</script>

 

Use Case

  • Useful for opening multiple reference links together.

Open Link in a New Tab with Styled Button

<a href="https://www.github.com" target="_blank" style="padding:10px; background-color:blue; color:white; text-decoration:none; border-radius:5px;">Visit GitHub</a>

 

Explanation:

  • This link is styled as a button for better user experience.

Using JavaScript to Open Links in a New Tab  

Sometimes, you may want to open links in a new tab dynamically using JavaScript. This is useful when the decision to open a link depends on user actions or specific conditions. Let’s understand this in detail: 

How It Works  

JavaScript provides a method called `window.open()` that allows you to open a URL in a new tab or window. You can trigger this method using events like button clicks or other interactions. For example:  

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Open Link in New Tab</title>
</head>
<body>
    <button id="openLinkButton">Click Me to Open Link</button>

    <script>
        // Select the button element
        const button = document.getElementById('openLinkButton');

        // Add a click event listener to the button
        button.addEventListener('click', function() {
            // Open a link in a new tab
            window.open('https://www.example.com', '_blank');
        });
    </script>
</body>
</html>

 

In this Code:   

1. HTML Structure: We create a simple button with the ID `openLinkButton`. This button will trigger the action.  
 

2. JavaScript Logic:  

  • The `document.getElementById()` method selects the button element.  
     
  • The `addEventListener()` method listens for a click event on the button.  
     
  • When the button is clicked, the `window.open()` method is executed.  
     
  • The first argument in `window.open()` is the URL you want to open (e.g., `https://www.example.com`).  
     
  • The second argument, `_blank`, tells the browser to open the link in a new tab.  

Why Use JavaScript?  

Using JavaScript gives you more control over when & how the link opens. For example, you can add conditions like opening the link only if a user is logged in or after a certain amount of time has passed.  

Why and When to Open Links in a New Tab  

Opening links in a new tab isn’t always necessary, but there are specific scenarios where it’s the best choice. Let’s explore why this feature is useful & when you should use it.  

Why Use This Feature?  

1. Keeps Users on Your Website: When a link opens in the same tab, users leave your page. By opening links in a new tab, they stay on your site while exploring additional content.  
 

2. Improves Navigation: If the link leads to external resources or references, users can easily switch back to your page without losing their place.  
 

3. Enhances User Experience: For example, if your website has a blog with external references, opening those references in a new tab allows users to read them later without interrupting their flow.  

When Should You Use It?  

  • External Links: If the link directs users to another website, it’s polite to open it in a new tab. This ensures they don’t lose track of your content.  
     
  • Downloads or Forms: If clicking a link triggers a file download or opens a form, opening it in a new tab prevents disruption to the current page.  
     
  • Reference Materials: If your content includes links to articles, tutorials, or documentation, opening them in a new tab lets users refer back to your content easily.  

Example: Opening External Links in a New Tab  

Let’s take a look at how you can modify your HTML to ensure all external links open in a new tab:  

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>External Links in New Tab</title>
</head>
<body>
    <p>Here is an example of an <a href="https://www.example.com" target="_blank" rel="noopener noreferrer">external link</a> that opens in a new tab.</p>

    <p>This is another <a href="https://www.google.com" target="_blank" rel="noopener noreferrer">link to Google</a>.</p>
</body>
</html>

 

In this Code:   

1. `target="_blank"`: This attribute tells the browser to open the link in a new tab.  
 

2. `rel="noopener noreferrer"`: This improves security & performance by preventing the new page from accessing the `window.opener` property of your site. It also avoids potential phishing attacks.  

Avoid Overusing This Feature  

While opening links in a new tab is helpful, overusing it can frustrate users. For example, if every link on your site opens in a new tab, users may end up with too many tabs open, leading to confusion. Use this feature wisely & only when it adds value.  

User Experience Considerations  

 

When deciding to open links in a new tab, it’s important to think about how it affects users. A good user experience ensures that your website is easy to navigate & doesn’t frustrate visitors. Let’s discuss the key considerations to keep in mind.  
 

1. Avoid Confusing Users  

Opening too many links in new tabs can overwhelm users. For instance, if every link on your site opens in a new tab, users may end up with dozens of tabs, making it hard to manage their browsing session. To avoid this:  

  • Only use `target="_blank"` for specific links like external resources or downloads.  
     
  • Make sure users know that clicking a link will open a new tab. You can add a small icon (like an arrow or a note) next to the link to indicate this behavior.  

 

2. Accessibility Matters  

Not all users interact with websites in the same way. Some rely on screen readers or keyboard navigation. When you open links in new tabs, ensure they are accessible:  

  • Use descriptive link text so users understand where the link leads. For example, instead of "Click here," use "Read more about HTML basics."  
     
  • Add `aria-label` attributes if necessary to provide additional context for screen readers.  

 

3. Security & Privacy  

Using `rel="noopener noreferrer"` is crucial when opening links in new tabs. Without this attribute, the new page can access information about your site through the `window.opener` property. This could lead to security risks like phishing attacks. For example: 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>User Experience Example</title>
</head>
<body>
    <p>Visit our partner site: 
        <a href="https://www.partnerwebsite.com" target="_blank" rel="noopener noreferrer">Partner Website</a>.
    </p>

    <p>For more details, check out this 
        <a href="https://www.externalresource.com" target="_blank" rel="noopener noreferrer">external resource</a>.
    </p>
</body>
</html>

 

Output

Output

 

In this Code:   

1. `target="_blank"`: Ensures the link opens in a new tab.  
 

2. `rel="noopener noreferrer"`: Protects user privacy & prevents potential security vulnerabilities.  
 

3. Descriptive Link Text: The text clearly explains where the link leads, improving accessibility.  

 

4. Mobile-Friendly Design  

On mobile devices, opening too many tabs can be frustrating because switching between tabs isn’t as smooth as on desktops. To improve mobile user experience:  

  • Limit the number of links that open in new tabs.  
  • Test your website on different devices to ensure it works well for all users.  

 

5. Provide Clear Instructions  

If you decide to open links in new tabs, let users know. For example, you can add a small note like "(opens in a new tab)" after the link. This sets clear expectations & avoids confusion.  

Frequently Asked Questions

Why should I use rel="noopener noreferrer" when using target="_blank"?

Using rel="noopener noreferrer" prevents security vulnerabilities, ensuring the new page does not manipulate the original page using JavaScript.

Can I make an image link open in a new tab?

Yes, wrapping an image inside an <a> tag with target="_blank" allows the image to act as a clickable link that opens in a new tab.

How do I force all links to open in a new tab?

JavaScript can be used to dynamically modify all anchor tags on a webpage to ensure they open in new tabs by setting their target attribute to _blank.

Conclusion

In this article, we learned how to open a link in a new tab in HTML using the target="_blank" attribute inside the <a> tag. This method ensures that the linked page opens in a new browser tab, improving navigation without disrupting the current page. Understanding this technique helps enhance user experience and website usability.

Live masterclass