mark smith
mark smith

Reputation: 20907

Popup blocked when created in jQuery ajax success callback

Google Chrome seems to be blocking a popup I am creating via jQuery. After some investigation, it appears to be an issue with calling window.open in the success event of an ajax call. Is there a way around this? My jQuery ajax call returns the URL to be opened. So I am bit stuck.

It works if I place the window.open outside the ajax call; but, inside (i.e. in the success event) it is blocked. I think it is something to do with CONTEXT but I am unsure.

Here is what I have:

     window.open("https://www.myurl.com");  // OUTSIDE OF AJAX - no problems 
                                            // with popup

     $.ajax({
        type: "POST",
        url: "MyService.aspx/ConstructUrl",
        data: jsonData,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            // Normally loads msg.d which is the url returned from service
            // static url below is for testing
            window.open("https://www.myurl.com");  // THIS IS BLOCKED
        },
        error: function(msg) {
            // alert(error);
        }
    });

Upvotes: 13

Views: 37629

Answers (9)

Rameshwor Maharjan
Rameshwor Maharjan

Reputation: 242

this works for me.

$(document).ready(function() {
        $('#myLink').on( "click", function() {
            var myNewTab = window.open('about:blank', '_blank');
            $.ajax({
                method: "POST",
                url: "ajax.php",
                data: { name: "John", location: "Boston" },
            })
            .done(function( msg ) {
                myNewTab.location.href = "google.com";
            });
        });
    });

Upvotes: 0

tiepnv
tiepnv

Reputation: 336

if you put async:true then you must do the following:

var safariOp = window.open("https://www.myurl.com"); //define before callback ajax contains url .
 $.ajax({
    type: "POST",
    url: "MyService.aspx/ConstructUrl",
    data: jsonData,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        //it's ok on safari
        safariOp.focus();
    },
    error: function(msg) {
        //alert(error);
    }
});

Upvotes: 0

tiepnv
tiepnv

Reputation: 336

 if you put async:true then you must do the following:

 var safariOP = window.open("https://www.myurl.com");  // DEFINE BEFORE AJAX CALLBACK 

 $.ajax({
    type: "POST",
    url: "MyService.aspx/ConstructUrl",
    data: jsonData,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
      if(safariOP){
         safariOP.focus(); // IT'S OK ON SAFARI
      }
    },
    error: function(msg) {
        //alert(error);
    }
});

Upvotes: 1

zachelrath
zachelrath

Reputation: 852

Follow the method described in this post:

window.open without popup blocker using AJAX and manipulating the window.location

In a nutshell, the approach is:

  1. Your code must be initiated by an onclick event.
  2. Open a new window, perhaps initially with no content, with window.open --- and store a reference to the window so that you can use it later.
  3. In the success callback from your AJAX operation, use window.location.replace to manipulate the URL of your already-open window with the desired URL.

Upvotes: 1

Andrew
Andrew

Reputation: 1653

As several people have pointed out, the accepted answer no longer works. Based on the comment by aidiakapi, I used a workaround by first opening the window.

window.open("about:blank", "myNewPage");

Then doing the ajax business and in the done() function, open the page with that name.

window.open("/foo.html", "myNewPage");

This also doesn't matter if you do it async or not.

Upvotes: 19

karim79
karim79

Reputation: 342635

Simply open the new window in the success callback:

 $.ajax({
    type: "POST",
    url: "MyService.aspx/ConstructUrl",
    data: jsonData,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        window.open("https://www.myurl.com"); 
    },
    error: function(msg) {
        //alert(error);
    }
});

Note you might have to set $.ajax's async option to false for that, otherwise the code following the $.ajax call could be evaluated before the response is received.

Upvotes: 9

Code.Town
Code.Town

Reputation: 1226

Your code returns these in firefox and chrome:

Firefox: "Firefox prevented this site from opening a pop-up window." Chrome: "Pop-up blocked"

To fix this issue just add async:false to your ajax call.

$.ajax({
type: "POST",
async: false,
url: "MyService.aspx/ConstructUrl",
data: jsonData,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(url) {
    window.open(url); 
},
error: function(msg) {
    //alert(error);
}

});

Just be careful that async:false will force javascript to wait for jQuery ajax result. It means it freezes javascript until ajax call is completed.

Upvotes: 7

Craig
Craig

Reputation: 2143

You can still have the code in the success event but async will need to be false.

Upvotes: 1

Matt Bridges
Matt Bridges

Reputation: 49385

Firefox does popup blocking based on the event that causes the javascript code to run; e.g., it will allow a popup to open that was triggered from an onclick, but not one that was triggered by a setTimeout. It has gotten a little bit complicated over the years as advertisers have tried to circumvent firefox's popup blocker.

Upvotes: 2

Related Questions