#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: | ||
Blocked by: | Blocking: |
Description (last modified by )
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 (2)
comment:1 Changed 16 years ago by
comment:2 Changed 16 years ago by
Description: | modified (diff) |
---|---|
Resolution: | → wontfix |
Status: | new → closed |
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.)
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 :