> > $().unload() - Removes load event handlers AND triggers unload event
>
> It actually doesn't do both, it just the last one that was
> created (which, I think, is unload).
Well of course it's unload. So is the first one - that's the problem! :-)
Here's the actual code from event.js in the order it executes, with hard
coded "var o" lines for clarity:
// $().unload(f) unbinds the 'load' event handler
var o = 'load';
jQuery.fn["un"+o] = function(f){ return this.unbind(o, f); };
// $().unload() triggers the 'unload' event
// $().unload(f) binds the 'unload' event handler
var o = 'unload';
jQuery.fn[o] = function(f){
return f ? this.bind(o, f) : this.trigger(o);
};
$().unload is actually defined to do three different things. The second
function overwrites the first one, so the two operations it provides are the
ones that will actually work.
The only sensible thing for a jQuery programmer to do is never, ever use
$().unload(). Instead, code the three different meanings explicitly.
Don't code this:
$().unload( f ); // I meant to unbind the 'load' handler...
$().unload( f ); // ...but I bound the 'unload' handler instead!
$().unload(); // Trigger an 'unload' event
Code this:
$().unbind( 'load', f );
$().bind( 'unload', f );
$().trigger( 'unload' );
That's only a bit more code, and I think it is a lot more readable. The
shorter code almost requires a comment to explain it, where the explicit
code is self-documenting.
-Mike
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/