Ticket #1365 (closed bug: wontfix)
Impossible to return a value other than false or undefined from an event handler
| Reported by: | rformato | Owned by: | |
|---|---|---|---|
| Priority: | minor | Milestone: | 1.1.4 |
| Component: | event | Version: | 1.1.3 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description (last modified by john) (diff)
Hi, I found that it's impossible to return a value other than false or undefined from an event handler.
In addition it's also useless to set a value for e.returnValue, as the event object is cloned in jQuery.event.fix
So, taking the onbeforeunload event as an example, it is not possible to return a string to show a message to the user before leaving a page.
It should be possible to fix this changing a bit jQuery.event.handle
from
if ( c[j].apply( this, args ) === false ) {
event.preventDefault();
event.stopPropagation();
val = false;
}
to
if ( (val=c[j].apply( this, args )) === false ) {
event.preventDefault();
event.stopPropagation();
}
Change History
comment:2 Changed 6 years ago by john
- Status changed from new to closed
- Resolution set to wontfix
- Description modified (diff)
returnValue is an IE specific feature - what if you just do:
$(window).bind("beforeunload"), function(){
return "Quit without saving?";
});
However, the problem is much larger than that - the beforeunload event ONLY works if you directly access the window object and do:
window.onbeforeunload = function(){ ... };
For example, opening a browser and doing:
window.addEventListener('beforeunload',function(){
return "test";
});
You'll find that it doesn't work, so we won't be able to do anything to help the situation. (So in this case, I recommend that you fall back to the traditional way of doing things.)
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

I've just tested the modification with this code :
$(window).bind("beforeunload", function(e) {
} );
And it's not better (it doesn't ask the question).
The precedent behavior is documented here :