How to Use HTML to Open a Link in a New Tab (2024)

/ #Browsers
How to Use HTML to Open a Link in a New Tab (2)

Tabs are great, aren't they? They allow the multitasker in all of us to juggle a bunch of online tasks at the same time.

Tabs are so common now that, when you click on a link, it's likely it'll open in a new tab.

If you've ever wondered how to do that with your own links, you've come to the right place.

The Anchor Element

To create a link on a web page, you need to wrap an anchor (<a>) element around text, then set its href attribute to the URL you want to link to.

<p>Check out <a href="https://www.freecodecamp.org/">freeCodeCamp</a>.</p>

Check out freeCodeCamp.

If you click on the link above, the browser will open the link in the current window or tab. This is the default behavior in every browser.

To open a link in a new tab, we'll need to look at some of the other attributes of the anchor element's other attributes.

The Target Attribute

This attribute tells the browser how to open the link.

To open a link in a new tab, just set the target attribute to _blank:

<p>Check out <a href="https://www.freecodecamp.org/" target="_blank">freeCodeCamp</a>.</p>

Now when someone clicks on the link, it will open up in a new tab, or possibly a new window depending on the person's browser settings.

Security concerns with target="_blank"

I strongly recommend that you always add rel="noreferrer noopener" to the anchor element whenever you use the target attribute:

<p>Check out <a href="https://www.freecodecamp.org/" target="_blank" rel="noopener noreferrer">freeCodeCamp</a>.</p>

This results in the following output:

Check out freeCodeCamp.

The rel attribute sets the relationship between your page and the linked URL. Setting it to noopener noreferrer is to prevent a type of phishing known as tabnabbing.

What is tabnabbing?

Tabnabbing, sometimes called reverse tabnabbing, is an exploit that uses the browser's default behavior with target="_blank" to gain partial access to your page through the window.object API.

With tabnabbing, a page that you link to could cause your page to redirect to a fake login page. This would be hard for most users to notice because the focus would be on the tab that just opened – not the original tab with your page.

Then when a person switches back to the tab with your page, they would see the fake login page instead and might enter their login details.

If you're interested in learning more about how tabnabbing works and what bad actors can do with the exploit, check out Alex Yumashev's article and this one by OWASP.

If you'd like to see a safe working example, check out this page and its GitHub repo for more information about the exploit and the rel attribute.

In summary

It's easy to use HTML to open a link in a new tab. You just need an anchor (<a>) element with three important attributes:

  1. The href attribute set to the URL of the page you want to link to,
  2. The target attribute set to _blank, which tells the browser to open the link in a new tab/window, depending on the browser's settings, and
  3. The rel attribute set to noreferrer noopener to prevent possible malicious attacks from the pages you link to.

Again, here's a full working example:

<p>Check out <a href="https://www.freecodecamp.org/" target="_blank" rel="noopener noreferrer">freeCodeCamp</a>.</p>

Which results in the following output in the browser:

Check out freeCodeCamp.

Thanks again for reading. Happy coding.

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

If you read this far, thank the author to show them you care.

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

ADVERTIsem*nT

How to Use HTML to Open a Link in a New Tab (2024)

FAQs

How to Use HTML to Open a Link in a New Tab? ›

To open a link in a new window, you can use the target attribute within an anchor tag in HTML. Set the value of the target attribute to "_blank" to specify that you want the link to open in a new window. This code will create a link that will open "https://www.example.com" in a new browser window or tab when clicked.

How to open link in new tab in HTML? ›

Using target="_blank" will open the link in a new tab or window.

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

The links. forEach(link => { link. setAttribute('target', '\_blank') }) sets the target attribute to _blank for each link, ensuring they open in a new tab.

How to make links open in a new tab? ›

The target attribute of a link element specifies where to open the linked URL. If you set target to "_blank" , the URL will usually open in a new tab.

How to open a link in the same tab in HTML? ›

HTML Links - The target Attribute

The target attribute can have one of the following values: _self - Default. Opens the document in the same window/tab as it was clicked. _blank - Opens the document in a new window or tab.

How do I open a new tab by clicking a button in HTML? ›

Add an HTML element to your Page Block: Write your own HTML link / button. It is this attribute (target="_blank") that causes the link to open in a new tab.

How do I open a link in a new tab in HTML react? ›

Implementing "Open Link in New Tab" Step-by-Step Guide
  1. a. Using Anchor Tags: This is the most common way to represent links in HTML. ...
  2. b. Using Custom Components: Some developers create custom components for links to maintain code consistency. ...
  3. a. Prevent Default Behavior: Use event. ...
  4. b. Open in New Tab: Utilize the window.
Feb 15, 2024

How do I open an action link in a new tab in HTML? ›

Set the value of the target attribute to "_blank" to specify that you want the link to open in a new window. This code will create a link that will open "https://www.example.com" in a new browser window or tab when clicked.

How do I open a link in a new tab without right clicking? ›

Open a new tab or window
  1. Windows & Linux: Ctrl + click a link.
  2. Mac: ⌘ + click a link.

How do I force a link to open in a new tab in CSS? ›

The short answer is: just add a target="_blank" attribute to your links (anchor tags). Now when your visitors click that link, it will open in a new window or tab(depending on which web browser they are using and how they configured that browser).

Why open links in a new tab? ›

Rationales I've encountered include: Ensuring users can easily return to the original site. Increasing time spent on the original site. Clearly differentiating between the original content and external resources.

How do I open a link in a new tab group? ›

Tips: To open a link in a new tab in a group, touch and hold the link, then tap Open in new tab in group. On your Android tablet: To add a tab into an existing tab group in the tab strip, drag the tab into the group.

How do I open an HTML link in a new tab? ›

You can use the target="_blank" attribute if you want your users to click on a link that opens up a new browser tab. The target="_blank" attribute is used inside the opening anchor tag like this.

How do I open links in different tabs? ›

Overview. Highlight any text and open all the included links at once, in new tabs. Just select the text containing links, right-click it, and select "Open links in new tabs".

What is the command to open link in another tab? ›

Ctrl+Enter

Opens the URL in a new tab.

How do I open a link in new tab markup? ›

You just add the target="_blank" attribute to your anchor tag so the links open up in a different browser tab.

How do I open a second page in HTML? ›

  1. Create Separate HTML Files:Each page of your website is a separate HTML file (e.g., index. html , about. html , contact. html ).
  2. Navigation Links:Add navigation links in your HTML files to connect pages.
  3. <nav> <a href="index.html">Home</a> <a href="about.html">About</a> <a href="contact.html">Contact</a> </nav>
Feb 15, 2021

How do I set Chrome to open links in a new tab automatically? ›

How to Use: Install the Extension: Add it to Chrome from the Chrome Web Store. Click the Icon: Find the extension icon in your toolbar and click it. Enable or Disable: Use the popup checkbox to turn the feature ON or OFF.

What is target _blank in HTML? ›

A target attribute with the value of “_blank” opens the linked document in a new window or tab. A target attribute with the value of “_self” opens the linked document in the same frame as it was clicked (this is the default and usually does not need to be specified).

Top Articles
FAQ | Binance FAQ
Les Meilleures Stratégies d'Investissem*nt pour les Débutants
English Bulldog Puppies For Sale Under 1000 In Florida
Forozdz
Noaa Charleston Wv
Chicago Neighborhoods: Lincoln Square & Ravenswood - Chicago Moms
Mountain Dew Bennington Pontoon
America Cuevas Desnuda
Costco The Dalles Or
Dr Lisa Jones Dvm Married
Co Parts Mn
How Far Is Chattanooga From Here
World of White Sturgeon Caviar: Origins, Taste & Culinary Uses
Craigslist Chautauqua Ny
Dumb Money
7440 Dean Martin Dr Suite 204 Directions
Comics Valley In Hindi
Marvon McCray Update: Did He Pass Away Or Is He Still Alive?
Jayah And Kimora Phone Number
Publix Super Market At Rainbow Square Shopping Center Dunnellon Photos
Silive Obituary
20 Different Cat Sounds and What They Mean
Wbiw Weather Watchers
Gina Wilson All Things Algebra Unit 2 Homework 8
Evil Dead Rise Showtimes Near Regal Sawgrass & Imax
Pirates Of The Caribbean 1 123Movies
If you have a Keurig, then try these hot cocoa options
About My Father Showtimes Near Copper Creek 9
Regal Amc Near Me
Section 408 Allegiant Stadium
Experity Installer
Inmate Search Disclaimer – Sheriff
Wega Kit Filtros Fiat Cronos Argo 1.8 E-torq + Aceite 5w30 5l
RUB MASSAGE AUSTIN
#1 | Rottweiler Puppies For Sale In New York | Uptown
Emerge Ortho Kronos
The TBM 930 Is Another Daher Masterpiece
Craigslist Ludington Michigan
Why I’m Joining Flipboard
2007 Jaguar XK Low Miles for sale - Palm Desert, CA - craigslist
Atom Tickets – Buy Movie Tickets, Invite Friends, Skip Lines
Kutty Movie Net
Hkx File Compatibility Check Skyrim/Sse
Darkglass Electronics The Exponent 500 Test
Walmart Careers Stocker
This Doctor Was Vilified After Contracting Ebola. Now He Sees History Repeating Itself With Coronavirus
Access to Delta Websites for Retirees
Workday Latech Edu
Blog Pch
Strange World Showtimes Near Century Federal Way
Ubg98.Github.io Unblocked
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 6400

Rating: 4.8 / 5 (78 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.