Reputation: 720
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. 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.
Upvotes: 2
Views: 6042
Reputation: 2720
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 claims the bug has been fixed, but in my testing it's still in jQuery UI 1.9.2.
http://bugs.jqueryui.com/ticket/8439 provides a possible workaround.
Upvotes: 2
Reputation: 6625
<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>
Upvotes: 2