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 pletely 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.
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 pletely 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.
Share Improve this question asked Apr 22, 2012 at 9:03 nvrsnvrs 7202 gold badges17 silver badges25 bronze badges 5- 1 it might be possible that the error is somewhere else, as this code snippet looks fine to me. Perhaps it is firing, but not showing the dialog? – 11684 Commented Apr 22, 2012 at 9:09
- to 11684: the event is definitely not firing, i tried printing out something to the console and i get nothing. Also i added a breakpoint and tried to debug but again whenever i leave the page it will not pause. The problem is somewhere else i guess but i do not mess with this event anywhere else in the whole page, unless one of the libraries does...hmmm – nvrs Commented Apr 22, 2012 at 9:26
-
Have you tried manually triggering this event (
$(window).trigger('beforeunload');
) and stepping through that to see where it takes you? Some other code might be rewriting your handler silently. Also, why are you binding it like this instead of through$('window').bind('beforeunload', function() {})
? – user1233508 Commented Apr 22, 2012 at 10:33 - @DCoder, i had tried triggering the event manually through jquery but i did it after i was doing various other stuff. Your suggestion got me to try it immediately after i bind the callback to it and of course it worked. The thing is, since i don't mess with the event but i use $('#container').delegate() and $('#container').on() quite i bit, could this be rewriting my handler? Cause i have looked at the various libs i use and i didn't find an offender – nvrs Commented Apr 22, 2012 at 14:56
- @DCoder, i was searching in my lib files for 'onbeforeunload' and couldn't find anything but you got me thinking and i found out that the version of this library valums./ajax-upload that i am using rewrites this event using jquery, i.e. qq.attach(window,"beforeunload",function(b){ Thanks – nvrs Commented Apr 22, 2012 at 15:00
2 Answers
Reset to default 2<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 autoplete feature of jQuery UI. Versions starting from 1.8.17 set unbeforeunload
, which disables existing handlers.
http://bugs.jquery./ticket/12061 claims the bug has been fixed, but in my testing it's still in jQuery UI 1.9.2.
http://bugs.jqueryui./ticket/8439 provides a possible workaround.