Open a new tab when clicking on a link
To open a new tab when clicking on a link, you can add the target
attribute to the link tag and set its value to _blank
. Here's an example:
<a rel="nofollow" target="_blank" href="https://www.example.com" target="_blank">Click me!</a>
This will open the link in a new tab when clicked.
Alternatively, you can also use the rel
attribute and set its value to noopener
to prevent the new tab from inheriting the permissions of the parent tab:
<a rel="nofollow" target="_blank" href="https://www.example.com" target="_blank" rel="noopener">Click me!</a>
This is a good practice to prevent potential security issues.
You can also use JavaScript to achieve this. For example:
<a rel="nofollow" target="_blank" href="https://www.example.com" onclick="window.open(this.href, '_blank'); return false;">Click me!</a>
This will open the link in a new tab when clicked, and prevent the default link behavior from occurring.
Note that the target
attribute only works for links that are clicked, not for links that are followed programmatically (e.g. using window.location.href
). If you need to open a link in a new tab programmatically, you can use the window.open
method:
window.open('https://www.example.com', '_blank');
This will open the link in a new tab.