coder9
coder9

Reputation: 1549

How to open a new browser tab from within a jQuery callback

I have tried the following to achieve this:

But neither works when calling window.open from inside the callback. Here's my code

$.post('api', { 
    }, function() {
    var win = window.open('target-file', '_blank');
    win.focus();
    });

Upvotes: 2

Views: 7720

Answers (4)

David Fawzy
David Fawzy

Reputation: 1076

ou can add this simple jQuery snipplet to your source to open every external link in a new tab of the web browser

$(document).ready(function(){
  $('a').each(function() {
    var a = new RegExp('/' + window.location.host + '/');
    if(!a.test(this.href)) {
      $(this).click(function(event) {
        event.preventDefault();
        event.stopPropagation();
        window.open(this.href, '_blank');
      });
    }
  });
});

Upvotes: 0

Sandy
Sandy

Reputation: 107

Try this:

 $.post('api', { 
    }, function() {
    window.open('target-file', '_blank');
   });

Upvotes: 2

Somnath Kharat
Somnath Kharat

Reputation: 3610

use MySurfaceWindow = window.open("url","windowname","settings");
see below example:

MySurfaceWindow = window.open('/DesktopModules/DMS/DMS.PatientEChart/Surface Pages/Surface6.aspx?SurfaceTooth=' + $("#lbl" + $(this).val()).text() + '&ProcedureID=0' + '&ColorD=I' + '', '', 'height=240,width=200,top=' + top + ',left=' + left + 'status=no,toolbar=no,menubar=no,location=no,resizable=no,scrollbars=no');

Upvotes: 0

PSR
PSR

Reputation: 40318

try it with the name of the window

Like

window.open("url", "NameOfNewWindow");

SEE HERE

Upvotes: 1

Related Questions