Skip to content Skip to sidebar Skip to footer
Reading Time: < 1 minute

If you’re working with javascript and html sometimes you want to open the url in browser new tab using javascript.Because that process is dynamically doing using javascript and can’t apply the html.For a example If I clicked on the link it open in new tab, but that clicking process is doing by javascript.I think you have an idea about that.OK, let’s see how to open a link in the new tab using javascript.

The normal method as following.But that is not open the new tab.It is opening as new window/popup window.

Google

This is the normal script and it is not open the new tab instead of the new window.

function openNewTab(){
 var myURL = 'http://google.com';
 window.open(myURL,'_blank');
}

NOTE:-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.

Method-1

function openNewTab(){
   var myURL = 'http://google.com';
   var win=window.open(myURL, '_blank');
   win.focus();
}

NOTE: So you could do it this way or by adding an event listener to your DOM object.

Method-2

     $("div.myDiv").on("click",function(){
         var myURL = 'http://google.com';
         window.open(myURL,'_blank');
     });

Also you can use $(this).attr('target', '_blank'); when clicked on that element.