我正在创建一个弹出窗口,其中安装了 beforeunload 处理程序。当使用“关闭”文件菜单项关闭弹出窗口时,beforeunload 处理程序被调用两次,从而导致出现两条“您确定要关闭此窗口吗?

这是 Firefox 的一个错误,我已经 在这里报告了, ,但我仍然想要一种方法来防止这种情况发生。你能想出一种合理的方法来检测双重卸载前防止双重消息问题吗?问题是 Firefox 没有告诉我用户选择单击对话框中的哪个按钮 - 确定或取消。

有帮助吗?

解决方案 3

这绝对是一个FF错误。我已经在 https://bugzilla.mozilla.org/show_bug.cgi? ID = 531199个

其他提示

<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>

在处理程序中设置变量以防止对话框第二次出现。之后使用setTimeout重置它。

我发现的最佳解决方案是使用一个标志全局变量,该变量在经过这么多毫秒后重置,比如说500(这可以确保可以再次调用该函数,但不会在它出现后立即调用)。

参见最后一段代码:

http:// social .msdn.microsoft.com /论坛/ EN / sharepointinfopath /线程/ 13000cd8-5c50-4260-a0d2-bc404764966d

我在 Chrome 21、Firefox 14、IE 7-9、Safari 5(PC 上)中发现了此问题。

以下内容适用于所有这些浏览器。如果在事件期间删​​除 window.onbeforeunload 函数,这将阻止第二次调用。诀窍是如果用户决定留在页面上,则重置 window.onbeforeunload 函数。

var window_on_before_unload = function(e) {
    var msg;
    // Do here what you ever you need to do
    msg = "Message for user";

    // Prevent next "window.onbeforeunload" from re-running this code.
    // Ensure that if the user decides to stay on the page that
    // this code is run the next time the user tries to leave the page.
    window.onbeforeunload = set_on_before_unload;

    // Prepare message for user
    if (msg) {
        if (/irefox\/([4-9]|1\d+)/.test(navigator.userAgent))
            alert(msg
                    + '\n\nThe next dialog will allow you to stay here or continue\nSee Firefox bug #588292');

        (e = e || window.event).returnValue = msg;
        return msg;
    }
};

// Set window.onbeforeunload to the above handler.
// @uses window_on_before_unload
// @param {Event} e
var set_on_before_unload = function(e) {
    // Initialize the handler for window.onbeforeunload.
    window.onbeforeunload = window_on_before_unload;
}

// Initialize the handler for window.onbeforeunload.
set_on_before_unload();

创建一个在处理程序中设置为true的全局变量。仅当此变量为false时才显示警报/弹出窗口。

我使用以下代码段来跟踪exitcount

当页面加载以下变量时,exitCount被初始化

if (typeof(MTG) == 'undefined') MTG = {};
MTG.exitCount=0; 

并在Window unload事件中

$(window).bind("beforeunload", function(){


            if (MTG.exitCount<=0) 
            {

                             //do your thing, save etc
            }   
            MTG.exitCount++;




});

我发现不是自己调用confirm(),而是执行even.preventDefault();在beforeunload事件中。 Firefox引发了自己的确认对话框。 我不确定这是否是正确/标准的事情,但这就是他们这样做的方式。

我有一个文档用window.open打开另一个弹出窗口。在原始窗口中,我已经注册(使用jquery)一个用于“卸载”的监听器。像这样的事件:

var popup_window = window.open(...) 
$(popup_window).on('unload', function(event) ...

我遇到过这个页面,因为该事件有效地触发了两次。我发现它不是一个bug,它会触发两次,因为它会触发一次“about:blank”。页面被您的页面替换,另一页面被卸载。

我所要做的就是通过查询原始事件来过滤我感兴趣的事件:

function (event) {
   var original_url = e.originalEvent.originalTarget.URL;
   if (original_url != 'about:blank')
   {
       ... do cool things ...
   }
}

我不知道这是否适用于原始问题,因为这是打开另一个窗口的特殊情况,但我希望它有所帮助。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top