javascript 在关闭当前选项卡之前打开弹出窗口
javascript open popup before closing the current tab
我想在用户关闭当前站点的最后一个选项卡时打开一个弹出窗口。我遇到的问题是
- 当浏览器提示用户(
onbeforeunload
返回一个非空值)时,弹出窗口被浏览器阻止(见下面的案例 1)
- 当浏览器不向用户提示任何内容(
onbeforeunload
不返回任何内容)时,弹出窗口根本不会打开并且似乎被浏览器完全忽略(这是为什么?)(见下面的案例 2)
案例 1:提示和弹出窗口被阻止(代码来自 this answer)
window.onbeforeunload = function (e) {
e = e || window.event;
window.open('http://localhost', '_blank');
// For IE and Firefox prior to version 4
if (e) {
e.returnValue = 'Sure?';
}
// For Safari
return 'Sure?';
};
情况二:无提示,无弹窗,页面关闭"normally"
window.onbeforeunload = function (e) {
e = e || window.event;
window.open('http://localhost', '_blank');
};
这是我的代码:
var popWindow = true;
window.onbeforeunload = function() {
if(popWindow == true) {
popWindow = false;
return "Sure?";
}
}
An algorithm is allowed to show a popup if any of the following conditions is true
The task in which the algorithm is running is currently processing an activation behavior whose click event was trusted.
window关闭不是点击事件。
The task in which the algorithm is running is currently running the event listener for a trusted event whose type is in the following list:
unload
不在该列表中。
The task in which the algorithm is running was queued by an algorithm that was allowed to show a popup, and the chain of such algorithms started within a user-agent defined timeframe.
For example, if a user clicked a button, it might be acceptable for a popup to result from that after 4 seconds, but it would likely not be acceptable for a popup to result from that after 4 hours.
同样,不。任务未排队。
无法打开弹出窗口来响应用户试图离开您的站点。它会导致太多有害行为(点击这个广告,退出,弹出,你必须点击这个广告,循环).
我想在用户关闭当前站点的最后一个选项卡时打开一个弹出窗口。我遇到的问题是
- 当浏览器提示用户(
onbeforeunload
返回一个非空值)时,弹出窗口被浏览器阻止(见下面的案例 1) - 当浏览器不向用户提示任何内容(
onbeforeunload
不返回任何内容)时,弹出窗口根本不会打开并且似乎被浏览器完全忽略(这是为什么?)(见下面的案例 2)
案例 1:提示和弹出窗口被阻止(代码来自 this answer)
window.onbeforeunload = function (e) {
e = e || window.event;
window.open('http://localhost', '_blank');
// For IE and Firefox prior to version 4
if (e) {
e.returnValue = 'Sure?';
}
// For Safari
return 'Sure?';
};
情况二:无提示,无弹窗,页面关闭"normally"
window.onbeforeunload = function (e) {
e = e || window.event;
window.open('http://localhost', '_blank');
};
这是我的代码:
var popWindow = true;
window.onbeforeunload = function() {
if(popWindow == true) {
popWindow = false;
return "Sure?";
}
}
An algorithm is allowed to show a popup if any of the following conditions is true
The task in which the algorithm is running is currently processing an activation behavior whose click event was trusted.
window关闭不是点击事件。
The task in which the algorithm is running is currently running the event listener for a trusted event whose type is in the following list:
unload
不在该列表中。
The task in which the algorithm is running was queued by an algorithm that was allowed to show a popup, and the chain of such algorithms started within a user-agent defined timeframe.
For example, if a user clicked a button, it might be acceptable for a popup to result from that after 4 seconds, but it would likely not be acceptable for a popup to result from that after 4 hours.
同样,不。任务未排队。
无法打开弹出窗口来响应用户试图离开您的站点。它会导致太多有害行为(点击这个广告,退出,弹出,你必须点击这个广告,循环).