Javascript open link in new tab

A simple but useful question!

To open a link in a new tab using JavaScript, you can use the target attribute and set it to _blank. Here's an example:

<a rel="nofollow" target="_blank" href="https://www.example.com" target="_blank">Open in new tab</a>

Alternatively, you can use JavaScript to achieve the same result:

<a id="myLink" href="https://www.example.com">Open in new tab</a>
<script>
  document.getElementById("myLink").addEventListener("click", function() {
    window.open(this.href, "_blank");
  });
</script>

In this example, we add an event listener to the link element that listens for a click event. When the link is clicked, the event listener calls the window.open() method, passing the link's href attribute as the first argument and _blank as the second argument, which tells the browser to open the link in a new tab.

You can also use the window.open() method without adding an event listener:

<a id="myLink" href="https://www.example.com">Open in new tab</a>
<script>
  window.open(document.getElementById("myLink").href, "_blank");
</script>

This will open the link in a new tab as soon as the page loads.

Note that the target attribute is not supported in HTML5, so if you're using HTML5, you should use the JavaScript approach instead.