How can I open a new window using a URL that is generated inside the getScript callback function, and avoid pop-up blockers?(如何使用在 getScript 回调函数中生成的 URL 打开新窗口,并避免弹出窗口阻止程序?)
问题描述
我遇到的问题是,当我尝试执行以下代码之类的操作时,窗口将被弹出窗口阻止程序阻止.我正在使用 getScript 以便可以发出跨域请求.我正在使用 jQuery 1.4.2 来执行以下操作.
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.
将被阻止的代码示例:
//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");
});
});
});
通过拦截器但未及时获取 URL 的代码示例:
//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");
});
});
如何使用在 getScript 回调函数中生成的 URL 打开一个新窗口,并避免弹出窗口拦截器?
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.
这种方式允许我在不需要处理 javascript 的中间页面的情况下进行弹出窗口.
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;
});
});
这篇关于如何使用在 getScript 回调函数中生成的 URL 打开新窗口,并避免弹出窗口阻止程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用在 getScript 回调函数中生成的 URL 打开新窗口,并避免弹出窗口阻止程序?


- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Flexslider 箭头未正确显示 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06