How to make an href open in a new tab
To make an <a>
link open in a new tab, you can add the target
attribute to the link and set its value to _blank
. Here's an example:
<a rel="nofollow" target="_blank" href="https://www.example.com" target="_blank">Link to open in new tab</a>
This will open the link in a new tab or window when clicked.
Alternatively, you can also use the rel
attribute and set its value to noopener
or noreferrer
to prevent the new tab from being able to access the parent page's cookies and other information:
<a rel="nofollow" target="_blank" href="https://www.example.com" target="_blank" rel="noopener">Link to open in new tab</a>
Or:
<a rel="nofollow" target="_blank" href="https://www.example.com" target="_blank" rel="noreferrer">Link to open in new tab</a>
Note that the noopener
and noreferrer
values are not supported in older browsers, so you may want to use the target
attribute alone if you need to support older browsers.
You can also use JavaScript to achieve this, by using the window.open()
method:
<a rel="nofollow" target="_blank" href="javascript:void(0)" onclick="window.open('https://www.example.com', '_blank')">Link to open in new tab</a>
This will open the link in a new tab when clicked, but it's not recommended as it can be confusing for users and may not work as expected in all browsers.