How to open a new window using the URL created inside the getScript callback function and avoid pop-up blocking?

The problem I am facing is when I try to do something like the code below, the window is blocked by pop-up blockers. I use getScript to execute cross domain requests. I am using jQuery 1.4.2 to execute below.

Example code to be blocked:

//Code that gets blocked by pop-up blockers
$(document).ready(function(){
    $(".popup").click(function(){
        $.getScript("URL_To_A_Javascript_File", function(){
            window.open("dynamicURL", "_blank");
        });
    });
});

An example of code that passes blockers but does not get the URL in time:

//This code will get past the pop-up blocker, but the var url won't be updated 
//with the dynamicURL before the window.open() fires in browsers 
//like safari or chrome.
$(document).ready(function(){
    var url;
    $(".popup").click(function(){
        $.getScript("URL_To_A_Javascript_File", function(){
            url = "dynamicURL";
        });
        window.open(url, "_blank");
    });
});

How to open a new window using the URL created inside the getScript callback function and avoid pop-up blocking?

+5
source share
3 answers

Well, it looks like I finally figured out how to do what I was trying to do.

, javascript.

var newWin;
$(document).ready(function(){
    $(".popup").click(function(){
        newWin = window.open();

        $.getScript("URL_To_A_Javascript_File", function() {
            newWin.location = "DynamicURL";
        });
        return false;
    });
});
+6

, .

, ( "click"), , : .

- "getScript" , script, , , .

, "getScript" . "click".

+1

, .

0

All Articles