window.open 和 $.ajax().done() 中的弹出窗口拦截器
window.open and pop-up blocker in $.ajax().done()
我通过 jQuery 接到一个 ajax 电话;完成后我需要在新标签页中打开 URL。
我写了这个简单的函数:
var openWin = function() {
window.open('/UrlToOpen', '_blank');
win.focus();
}
如果我直接从 JS 代码调用此函数,它会在不触发弹出窗口阻止程序的情况下打开。
如果我从 $.ajax().done() 调用它,就像这样:
$.ajax({
url: 'ajaxUrl',
type: 'POST'
}).done(function (result) {
openWin();
});
弹出窗口拦截器已触发。
此处演示:https://jsfiddle.net/dggwL5uj/
为什么?我怎样才能避免这种情况?
如果将您的 ajax 请求更改为同步,这可能会起作用。
$.ajax({
url: 'ajaxUrl',
type: 'POST',
async: false,
}).done(function (result) {
openWin();
});
如评论中所述,如果打开 tab/popup 的命令来自受信任的事件,浏览器只会打开 tab/popup 而不会弹出窗口阻止程序警告。
我通过 jQuery 接到一个 ajax 电话;完成后我需要在新标签页中打开 URL。
我写了这个简单的函数:
var openWin = function() {
window.open('/UrlToOpen', '_blank');
win.focus();
}
如果我直接从 JS 代码调用此函数,它会在不触发弹出窗口阻止程序的情况下打开。
如果我从 $.ajax().done() 调用它,就像这样:
$.ajax({
url: 'ajaxUrl',
type: 'POST'
}).done(function (result) {
openWin();
});
弹出窗口拦截器已触发。
此处演示:https://jsfiddle.net/dggwL5uj/
为什么?我怎样才能避免这种情况?
如果将您的 ajax 请求更改为同步,这可能会起作用。
$.ajax({
url: 'ajaxUrl',
type: 'POST',
async: false,
}).done(function (result) {
openWin();
});
如评论中所述,如果打开 tab/popup 的命令来自受信任的事件,浏览器只会打开 tab/popup 而不会弹出窗口阻止程序警告。