The issue I am having is when I try to do something like the below code, the window will be blocked by pop-up blockers. I am using getScript so that I can make cross domain requests. I am using jQuery 1.4.2 to do the below.
Example of code that will 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");
});
});
});
Example of code that gets past blockers, but doesnt get 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 can I open a new window using a URL that is generated inside the getScript callback function, and avoid pop-up blockers?
Ok, it looks like I finally figured out how to do what I was trying to do.
This way allows me to do the pop-up with out the need for an intermediate page that handles the javascript.
var newWin;
$(document).ready(function(){
$(".popup").click(function(){
newWin = window.open();
$.getScript("URL_To_A_Javascript_File", function() {
newWin.location = "DynamicURL";
});
return false;
});
});
You can't avoid popup blockers, and let us all give thanks for that.
When your code opens a window from some event loop that's not the direct result of user action (mostly that means a "click" event), the browser assumes that the user should have a choice of whether to see the new window.
In the case of something like your "getScript", the handler that's called when the script has been gotten is in one of those kinds of non-user event loops, so the blocker rules apply.
You could, perhaps, run your "getScript" code from your new window. The browser will allow the window to be opened from that "click" handler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With