How to Open Link in a New Tab in Html

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> | attribute target
  • 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



Leave a reply



Submit