How to make href open in new tab
To make a link (href
) open in a new tab, you can add the target
attribute to the link and set its value to _blank
. Here are a few ways to do it:
Method 1: Using the target
attribute
HTML:
<a rel="nofollow" target="_blank" href="https://www.example.com" target="_blank">Open in new tab</a>
Method 2: Using the rel
attribute
HTML:
<a rel="nofollow" target="_blank" href="https://www.example.com" rel="noopener noreferrer" target="_blank">Open in new tab</a>
The rel
attribute is optional, but it's a good practice to include it to prevent the new tab from being able to access the parent window's content.
Method 3: Using JavaScript
JavaScript:
<a rel="nofollow" target="_blank" href="https://www.example.com" onclick="window.open(this.href, '_blank'); return false;">Open in new tab</a>
This method uses the window.open
function to open the link in a new tab. The return false
statement prevents the default link behavior from occurring.
Method 4: Using a library or framework
If you're using a library or framework like jQuery, you can use its built-in methods to open the link in a new tab. For example:
<a rel="nofollow" target="_blank" href="https://www.example.com" onclick="$('a').attr('target', '_blank');">Open in new tab</a>
Note that the target
attribute should be added to the link element itself, not to the link text.