Side navigation
#1365 closed bug (wontfix)
Opened July 06, 2007 04:55PM UTC
Closed July 15, 2007 02:13PM UTC
Last modified March 14, 2012 04:31AM UTC
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
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(); }
Attachments (0)
Change History (2)
Changed July 06, 2007 09:38PM UTC by comment:1
Changed July 15, 2007 02:13PM UTC by comment:2
description: | 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(); \ } \ }}} → 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(); \ } \ }}} \ {{{ \ \ }}} \ |
---|---|
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) {
e.returnValue = 'Quit without saving ?'
} );
And it's not better (it doesn't ask the question).
The precedent behavior is documented here :