Hacker News new | past | comments | ask | show | jobs | submit login

> As lukashed said jQuery most likely interprets [undefined] as false and stop propagation of the event...

No, it doesn't. jQuery uses a strict test for false and does not stop propagation for other falsy return values from an event listener.

The documentation could be more clear on this point. All it says is: "Returning false from an event handler will automatically call event.stopPropagation() and event.preventDefault()."

http://api.jquery.com/on/#event-handler

Here's the code that does this check:

  if ( ret !== undefined ) {
      if ( (event.result = ret) === false ) {
          event.preventDefault();
          event.stopPropagation();
      }
  }
https://github.com/jquery/jquery/blob/master/src/event.js#L3...

Reading that code, it almost seems redundant at first to have a !== undefined check when the === false is already a strict comparison. But there is that assignment hidden inside the if expression. So the code is really the same as this more clearly written version:

  if ( ret !== undefined ) {
      event.result = ret;
      if ( ret === false ) {
          event.preventDefault();
          event.stopPropagation();
      }
  }
This would also have the same effect:

  if ( ret !== undefined ) {
      event.result = ret;
  }
  if ( ret === false ) {
      event.preventDefault();
      event.stopPropagation();
  }
These all do the same thing: set event.result only if ret is not undefined, and then call preventDefault and stopPropagation only if ret is false (and not just a falsy value).



Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: