How to open link in a new tab in HTML?
Set the target
attribute of the link to _blank
:
<a href="#" target="_blank" rel="noopener noreferrer">Link</a>
For other examples, see here: http://www.w3schools.com/tags/att_a_target.asp
Note
I previously suggested blank
instead of _blank
because, if used, it'll open a new tab and then use the same tab if the link is clicked again. However, this is only because, as GolezTrol pointed out, it refers to the name a of a frame/window, which would be set and used when the link is pressed again to open it in the same tab.
Security Consideration!
The rel="noopener noreferrer"
is to prevent the newly opened tab from being able to modify the original tab maliciously. For more information about this vulnerability read the following articles:
- The target="_blank" vulnerability by example
- External Links using target='_blank'
Open link in new tab or window
You should add the target="_blank"
and rel="noopener noreferrer"
in the anchor tag.
For example:
<a target="_blank" rel="noopener noreferrer" href="http://your_url_here.html">Link</a>
Adding rel="noopener noreferrer"
is not mandatory, but it's a recommended security measure. More information can be found in the links below.
Source:
- MDN | HTML element
<a>
| attributetarget
- About rel=noopener
- Opens External Anchors Using rel="noopener"
JavaScript: How to open a link in a new tab without window.open()?
I believe, the "noopener" feature runs the window in a seperate process
https://developer.mozilla.org/en-US/docs/Web/API/Window/open#noopener
let d = window.open('https://www.google.com/', 'name', 'noopener=yes');
with noopener, d would now be null
Open every link in new tab
Are you looking for a CSS solution only? or any solution like the one below (JQuery)
$('a').click(function() {
$(this).attr('target', '_blank');
});
And also you can add the code below in your head
tag-
<base target="_blank">
this will make all the a
tags open in a new tab
Related Topics
Why Do Browsers Insert Tbody Element into Table Elements
What to Use in Place of ::Ng-Deep
How to Add a New Line in Textarea Element
Can an Option in a Select Tag Carry Multiple Values
Using Sections in Editor/Display Templates
Maintain Aspect Ratio of Div But Fill Screen Width and Height in Css
Svgs Not Scaling Properly in Ie - Has Extra Space
Heights Rendering Differently in Chrome and Firefox
How to Make a Pure CSS Based Dropdown Menu
Properly Sizing and Aligning the Flex Item(S) on the Last Row
What's the Difference Between Disabled="Disabled" and Readonly="Readonly" for HTML Form Input Fields
Change Select List Option Background Colour on Hover
Html Encoding Issues - "Â" Character Showing Up Instead of "&Nbsp;"
How to Use Colspan and Rowspan in HTML Tables
Show an Image Preview Before Upload
Css - How to Style a Selected Radio Buttons Label