How to Open a New Tab in Javascript?

Here coder in this blog post we learn how to open a new tab in javascript on button click.

In most cases, this should happen directly in the onclick handler for the link to prevent pop-up blockers and the default “new window” behavior. You could do it this way, or by adding an event listener to your DOM object.


<a href="javascript:;" onclick="openInNewTab('')" >Open in new tab</a>


    function openInNewTab(url) {, '_blank').focus();