$.on(“ready”, handler) shouldn’t be deprecated and should behave the same as $(handler)

From http://api.jquery.com/ready/

There is also $(document).on( “ready”, handler ), deprecated as of jQuery 1.8. This behaves similarly to the ready method but if the ready event has already fired and you try to .on( “ready” ) the bound handler will not be executed. Ready handlers bound this way are executed after any bound by the other three methods above.

Wait, what??

So, I have to remember that I can’t use $(window).load(handler), I have to use $(window).on("load", handler) instead; but on the other hand, for the ready event it’s the other way round: I can’t use $(document).on("ready", handler), I should use $(document).ready(handler) or $(handler) instead. And also, $(handler) is the same as $(document).ready(handler) but it is not the same as $(document).on("ready", handler). Unlike every other event, where $(something).on("event", handler) is the same as $(something).event(handler).

That’s a lot of gratuitous inconsistencies and a lot of unnecessary things to know that one can easily miss.

$(document).on("ready", handler) should not be deprecated, and it should behave the same as $(handler), both in whether it is fired or not and in the order of execution.

I know, you’ll tell me that it is not gratuitous and unnecessary, that it is because of how stuff is implementated. However, implementation difficulties should dealt with by implementors, not exposed to the API users.

Author: Fantashit

1 thought on “$.on(“ready”, handler) shouldn’t be deprecated and should behave the same as $(handler)

  1. This is not about implementation difficulties. It is about a very big difference in the actual APIs. The load event is an actual event, so you use the event system’s API’s, e.g., .on(). There is no actual ready event. There is a document ready promise. This is why it’s safe the use $(handler) at any time, but late bindings to $(window).on('load', handler) won’t ever be fired (because you’ve missed the event actually occurring).

    $(document).on('ready', handler) was a mistake because it forced non-event functionality into the event system’s API. The closest event equivalent would be $(document).on('DOMContentLoaded', handler) but that would have the same late-binding problem as $(window).on('load', handler). The “ready event” is a convenience for developers so that you never have to worry about whether your code is running before or after the document is ready. The fact that it was referred to as an event for so long has caused a lot of confusion about this in the community. The deprecation of the use of ready through the event system should help developers understand what they’re actually doing.

    It’s also a bit late to even have a discussion about deprecation since the deprecation happened two and half years ago.

Comments are closed.