如何在未显示卸载之前显示模式对话框?

问题描述 投票:2回答:2

我确实知道无法用自定义对话框替换beforeunload对话框,并且,如果我们需要向用户设置自定义消息,则必须在beforeunload处理程序中返回一个字符串:

{Custom message here set by returning a string in our beforeunload handler}
Are you sure you want to leave this page?
[Leave this page] [Stay on this page]

因此,如何在浏览器显示实际的beforeunload对话框之前显示自定义模式对话框(也许是jQuery?)>

我当前的代码使用Fancybox:

window.onbeforeunload = function() {
    $.fancybox({ 'type':'iframe', 'href':'/PopupOnExit.php' });
    return "Special offer! Stay on this page for more details.";
};

但是,这首先显示浏览器的对话框,只有在单击“停留”或“离开”按钮之后,浏览器才会显示我的模式对话框。

有什么方法可以让我的模式对话框在浏览器对话框之前显示?

我知道,无法用自定义对话框替换beforeunload对话框,并且,如果我们需要向用户设置自定义消息,则必须在beforeunload中返回一个字符串...

javascript jquery dom-events onbeforeunload
2个回答
3
投票

仅当脚本结束执行时,DOM修改才会生效。在这种情况下,出于明显的安全原因,将首先触发本机对话框。

请注意,由于此未指定功能(请参阅the MDN doc)引入的许多安全问题,可能会删除它(我认为最快的是最好的),而过时的理由(保存数据)已过时在ajax时代。


0
投票

unload和onBeforeUnload并不是跨浏览器事件。小心。在Opera中,有时在Chrome中,该功能不起作用。

© www.soinside.com 2019 - 2024. All rights reserved.