How to make html link open in new tab
To make an HTML link open in a new tab, you can add the target
attribute to the a
tag and set its value to _blank
. Here's an example:
<a rel="nofollow" target="_blank" href="https://www.example.com" target="_blank">Visit Example.com</a>
This will open the link in a new tab or window when clicked.
Here are a few things to keep in mind:
- The
_blank
value tells the browser to open the link in a new tab or window. - You can also use
_new
instead of_blank
, but both values achieve the same result. - If you want to specify a specific window or tab to open the link in, you can use a number instead of
_blank
. For example,target="2"
would open the link in the second tab. - Some browsers may not support opening links in new tabs or windows if the user has disabled this feature. In this case, the link will open in the current tab or window.
You can also use JavaScript to achieve this, by adding an event listener to the link and using the window.open()
method:
<a rel="nofollow" target="_blank" href="https://www.example.com" onclick="window.open(this.href, '_blank'); return false;">Visit Example.com</a>
This will also open the link in a new tab or window when clicked. The return false
statement is used to prevent the default link behavior (i.e., opening the link in the current tab or window).