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?
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?
Share Improve this question edited Apr 21, 2015 at 0:41 Adam Link 2,7934 gold badges32 silver badges44 bronze badges asked Nov 4, 2010 at 13:45 Barry TaylorBarry Taylor 1131 silver badge7 bronze badges 2-
fi youre just using this for XD, why do you need to use
window.open
? – prodigitalson Commented Nov 4, 2010 at 13:51 - The reason I need to use XD ajax is because the javascript file I am calling is hosted in a Marketing CRM, and is returning a script that contains information about the user which I need to determine what page to pop-up. – Barry Taylor Commented Nov 4, 2010 at 14:27
3 Answers
Reset to default 6Ok, 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.
Simply don't work with popup, use something like Lightbox instead.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742362334a4429633.html
评论列表(0条)