我正在创建一个安装了beforeunload处理程序的弹出窗口。当使用“关闭”文件菜单项关闭弹出窗口时,两次调用beforeunload处理程序,结果为两个“确定要关闭此窗口吗?”。消息出现。

这是Firefox的一个错误,我安装了reported it here,但是我仍然希望有一种方法来防止这种情况的发生。您能想到一种明智的方法来检测双重预卸载以避免双重消息问题吗?问题是Firefox不会告诉我用户选择单击对话框中的哪个按钮-确定或取消。

最佳答案

<script type="text/javascript">
var onBeforeUnloadFired = false;

window.onbeforeunload = function ()
{
    if (!onBeforeUnloadFired) {
        onBeforeUnloadFired = true;
        event.returnValue = "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?";
   }

   window.setTimeout("ResetOnBeforeUnloadFired()", 10);
}

function ResetOnBeforeUnloadFired() {
   onBeforeUnloadFired = false;
}
</script>

07-28 10:51