window.onbeforeunload 无法在 Firefox 46 上弹出 window,所有其他浏览器都可以

window.onbeforeunload not working on Firefox 46 in a popup window, all other browsers work

Firefox 46 有问题,但在所有其他浏览器上运行正常。 退出页面后,我会问以下问题。 Firefox 完全忽略它。 请帮忙!

window.onbeforeunload = ThisCheckExittingPage;

var ThisCheckExitWindow = 1;

// Checks before exitting
// ThisCheckExitWindow = 1;

// Does NOT check before exitting
// ThisCheckExitWindow = 0;

function ThisCheckExittingPage() {

    if (ThisCheckExitWindow == 1)
    {
        return "You are about to exit this page.";
    }
}

这看起来像是设计使然,因为 WindowEventHandlers.onbeforeunload - Web APIs | MDN 有这条注释:

To combat unwanted pop-ups, browsers may not display prompts created in beforeunload event handlers unless the page has been interacted with. Firefox has implemented this policy since Firefox 44 (Bugzilla).

relevant patch 所示,mUserHasInteracted 标志变量在 window 中发生鼠标或键盘事件后设置。