简体   繁体   English

window.onbeforeunload不触发

[英]window.onbeforeunload not firing

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. 我已经创建了一个相当重的Ajax Web应用程序,并且我正在使用window.onbeforeunload事件来检测用户是否存在未保存的更改,并阻止她离开页面。 The code is loaded by my init function and it used to work on all browsers that support the event. 该代码由我的init函数加载,并且可以在支持该事件的所有浏览器上使用。 However, suddenly the onbeforeunload event stopped firing on every browser for no apparent reason. 但是,突然在没有明显原因的情况下,onbeforeunload事件停止在每个浏览器上触发。 i am using jquery 1.7.1 and there are a lot of events attached to various elements (either via delegation or directly). 我正在使用jquery 1.7.1,并且有很多事件附加到各种元素上(通过委托或直接)。 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;
  };
});

ps 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. ps即使我从回调中完全删除了检查并总是返回一个字符串,每次应该提示一条消息,它仍然不会触发。 I have checked that my browsers are working correctly with the event with simple pages that bind a callback to it. 我检查了我的浏览器是否可以通过将回调函数绑定到其上的简单页面来正常处理该事件。

<script>
window.onbeforeunload = function (e) {
    e = e || window.event;

    // For IE and Firefox prior to version 4
    if (e) {
        e.returnValue = 'Any string';
    }

    // For Safari
    return 'Any string';
};
</script>

A possible cause for this is using the autocomplete feature of jQuery UI. 可能的原因是使用jQuery UI的自动完成功能。 Versions starting from 1.8.17 set unbeforeunload , which disables existing handlers. 从1.8.17开始的版本设置了unbeforeunload ,它将禁用现有的处理程序。

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://bugs.jquery.com/ticket/12061声称该错误已修复,但在我的测试中,它仍在jQuery UI 1.9.2中。

http://bugs.jqueryui.com/ticket/8439 provides a possible workaround. http://bugs.jqueryui.com/ticket/8439提供了一种可能的解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM