views:

2932

answers:

2

Hi there,

can anyone help, I have some jquery and chrome is blocking the popup i am creating, after some investigation it appears to be an issue with window.open in sucess event of an ajax call.. Is there a way round this? My jquery ajax call needs to return the URL i need to open :-) ... so i am bit stuck...

If i place the open.window outside of the ajax call that it works, but inside (success) it is blocked... i think it is something to do with CONTEXT but i am unsure ..

Any ideas really appreciated... Thank you.

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 that is the url that is returned from service but put static url in for testing
            window.open("https://www.myurl.com");  // THIS IS BLOCKED
        },
        error: function(msg) {
            //alert(error);
        }
    });
+1  A: 

Have you tried something like:

var success = false;

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

if(success) {
   window.open("https://www.myurl.com"); 
   //blah blah blah
}

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.

karim79
Excellent! it works, thanks alot!
mark smith
karim, you just saved my day!
jodeci
+1  A: 

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.

Matt Bridges