如何在beforeunload显示自己之前显示模态对话框? [英] How to show a modal dialog before beforeunload shows its own?

查看:394
本文介绍了如何在beforeunload显示自己之前显示模态对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实理解用自定义的对话框替换 beforeunload 对话框是不可能的,如果我们需要为用户设置自定义消息,我们将会必须在我们的beforeunload处理程序中返回一个字符串:

I do understand that it's not possible to replace the beforeunload dialog with a custom one, and that if we need to set a custom message to the user, we'll have to return a string in our beforeunload handler:

{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)?

So, how about showing a custom modal dialog (maybe jQuery) before the actual beforeunload dialog is shown by the browser?

我当前的代码使用Fancybox:

My current code uses Fancybox:

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

然而,这首先显示浏览器的对话框,并且只有在点击停留或离开后才会显示按钮是否会显示我的模态对话框。

However, this shows the browser's dialog first, and only after clicking either "Stay" or "Leave" buttons does the browser show my modal dialog.

有没有办法让我的模态对话框显示在浏览器的对话框之前?

Is there any way to make my modal dialog show before the the browser's dialog?

推荐答案

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

The DOM modifications take effect only when your script ends execution. In this case, the native dialog is fired first for obvious security reason.

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

Note that due to the many security problem introduced by this unspecified feature (see the MDN doc), it will maybe be removed (the soonest the best in my opinion), the old reason to have it (save the data) being obsolete in the age of ajax.

这篇关于如何在beforeunload显示自己之前显示模态对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆