$(window).beforeunload() clobbers previous handler and return values
|Reported by:||dmethvin||Owned by:|
Moved from: http://bugs.jqueryui.com/ticket/8439
No matter what $(window).beforeunload(fn) does, it cannot interact reliably with a mix of native inline functions. For other events, jQuery attaches a private native handler via addEventListener or attachEvent and manages jQuery handlers in its own data structures. Here we are all trying to share a single window.onbeforeunload property, and it can't work.
window.onbeforeunload = fn1; $(window).on("beforeunload", fn2); $(window).on("beforeunload", fn3);
Currently, we clobber fn1 and it is never called; only fn2 and fn3 would be called.
window.onbeforeunload = fn1; $(window).on("beforeunload", fn2); $(window).on("beforeunload", fn3); window.onbeforeunload = null;
If the code that sets fn1 later tries to remove it by nulling out the handler, fn2 and fn3 don't run.
Having jQuery and the native code save any existing handler allows everyone to run, but prevents anyone from removing their handler; perhaps that is less of a problem in real-life scenarios.
There is also the return value problem. Unlike other event handlers that return false as a flag, the onbeforeunload event returns a string that is supposed to be displayed to the user, such as "Are you sure you want to leave this page? Your edits will be lost". In the case that there are multiple handlers what should be displayed to the user? Right now, we display the string from the *last* jQuery handler called.
Change History (11)
comment:1 Changed 4 years ago by dmethvin
- Component changed from unfiled to event
- Milestone changed from None to 1.9
- Priority changed from undecided to low
- Status changed from new to open
comment:3 follow-up: ↓ 4 Changed 4 years ago by Oleg
- Resolution set to fixed
- Status changed from open to closed