window.onbeforeunload没有开火 [英] window.onbeforeunload not firing

查看:81
本文介绍了window.onbeforeunload没有开火的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个相当ajax-heavy的网络应用程序,我正在使用 window.onbeforeunload 事件来检测用户是否有未保存的更改并阻止她导航远离页面。代码由我的init函数加载,它用于支持该事件的所有浏览器。然而,突然onbeforeunload事件在没有明显原因的情况下停止在每个浏览器上触发。我正在使用jquery 1.7.1并且有很多事件附加到各种元素(通过委托或直接)。有没有人知道这里可能出现什么问题?这是一个代码片段:

I have created a fairly ajax-heavy web app and i am using the window.onbeforeunload event to detect if there are unsaved changes by the user and prevent her from navigating away from the page. The code is loaded by my init function and it used to work on all browsers that support the event. However, suddenly the onbeforeunload event stopped firing on every browser for no apparent reason. i am using jquery 1.7.1 and there are a lot of events attached to various elements (either via delegation or directly). Does anyone have a clue what might be the problem here? Here is a code snip:

$(document).ready( function() {
  window.onbeforeunload = function(e) {
    if($(window).data("confirm") > 0)
        return "You have unsaved changes";
    else
        return null;
  };
});

p.s。即使我从回调中完全删除了检查并且总是返回一个字符串,它应该每次都提示一条消息,但它仍然不会触发。我已经检查过我的浏览器是否正常使用简单的页面绑定回调函数。

p.s. Even if i completely remove the check from within the callback and always return a string, which should prompt a message every time, it still does not fire. I have checked that my browsers are working correctly with the event with simple pages that bind a callback to it.

推荐答案

可能的原因因为这是使用jQuery UI的自动完成功能。从1.8.17开始的版本设置 unbeforeunload ,它会禁用现有的处理程序。

A possible cause for this is using the autocomplete feature of jQuery UI. Versions starting from 1.8.17 set unbeforeunload, which disables existing handlers.

http://bugs.jquery.com/ticket/12061 声称该错误已得到修复,但在我的测试中它仍然在jQuery UI中1.9.2。

http://bugs.jquery.com/ticket/12061 claims the bug has been fixed, but in my testing it's still in jQuery UI 1.9.2.

http://错误。 jqueryui.com/ticket/8439 提供了一种可行的解决方法。

http://bugs.jqueryui.com/ticket/8439 provides a possible workaround.

这篇关于window.onbeforeunload没有开火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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