On line 3331 version 1.7.1 in jQuery.event.dispatch
:
ret = ( (jQuery.event.special[ handleObj.origType ] || {}).handle || handleObj.handler ) .apply( matched.elem, args ); if ( ret !== undefined ) { event.result = ret; if ( ret === false ) { event.preventDefault(); event.stopPropagation(); } }
There was a lot of packaging before this line, but basically it runs the handler function (either the raw function or the memeber handler
handlerObject
) using apply
. If the result of this call is false, it performs preventDefault
and stopPropagation
.
This is mentioned in the documentation for on()
:
Returning false
from the event handler will automatically call event.stopPropagation()
and event.preventDefault()
.
Why did they do it? I donβt know, since I am not a jQuery development team, but I assume that only return false
much faster to type than event.preventDefault(); event.stopPropagation();
event.preventDefault(); event.stopPropagation();
. (And if jQuery isn't about having less to print, I'm not sure what that means.)
I do not believe that the return value of the event handler is really used everywhere in POJS (someone is right if it's wrong!). Thus, jQuery can safely cause the return
cause side effects in the handler (since returning false in the POJS handler is pointless, no POJS function will suffer).
source share