Monday, September 20, 2010

JavaScript secrets revealed by Internet: event propagation control

I just read an awesome article on the proper way to prevent event propagation in JavaScript.

jQuery Events: Stop (Mis)Using Return False

Summary:

return false;

is really shorthand for

event.preventDefault(); event.stopPropagation(); return;

I never knew about these, but I'll be sure to use them from now on.  I've had issues with event propagation in the past, and this article explains why very clearly. It even managed to explain what the heck an event.stopImmediatePropagation() would do.

I guess this means I have to remember to add the event object parameter to my callback functions now.

function myCorrectCallback(event) {
    //--- do fancy stuff

     event.preventDefault();
}

No comments:

Post a Comment