javascript 在关闭当前选项卡之前打开弹出窗口

javascript open popup before closing the current tab

我想在用户关闭当前站点的最后一个选项卡时打开一个弹出窗口。我遇到的问题是

案例 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?"; 
       }
  }

http://jsfiddle.net/anam/su8dznoa/

来自the specification

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.

同样,不。任务未排队。

无法打开弹出窗口来响应用户试图离开您的站点。它会导致太多有害行为(点击这个广告,退出弹出,你必须点击这个广告,循环).