Opening window into tab with window.open() in Chrome

Use this method when you need to open a new window into a tab and not into a popup window through javascript. This should work in most browsers if settings have not been altered.


function open_in_tab(url) {
   var win = window.open(url, '_blank');
   win.focus();
}

Opening window into tab after Ajax Call

window.open() will only open into a tab if it’s result of direct user action. Must make javascript think clicking button is the result of button click after the ajax call. Danger of running ajax synchronously is page can freeze waiting for the request. You can open a window into a tab after an AJAX call with the following:


$('#btn').on("click", function(){
    $.ajax({
      type: 'POST',
      url: "ajax.php",
      data: {action: 'update'},
      dataType: 'json',
      context: document.body,
      async:false,   //Send Synchronously
      success: function(resp){  
         if(resp.success) {
        	window.open('http://google.com'); //Will open into a tab
         }
      }
    });
    
})

Leave a comment

1 Comments.