The documentation of
.ready() is called after the DOM has been initialized, the new handler passed in will be executed immediately.
...and by "immediately" it means "synchronously."
Which is a problem: The handler should always be called asynchronously, even if the DOM is ready. This prevents
ready from having chaotic behavior. For instance, it's impossible to look at this code and determine the order in which "B" and "C" are logged:
In the normal case (DOM not yet ready when
ready is called), we'll get
...but if the DOM is already ready, we'll get
This allows bugs to slip in, where there is an undetected dependency on the order of B and C and a script is only tested in one situation or the other.
Somewhat gratuitous example showing the three situations in which
ready might be called (1. DOM not ready; 2. During another
ready handler; 3. DOM ready), and how they behave: http://jsbin.com/ezowij The output is:
Note the third is different. My belief is that if a callback ''may'' be asynchronous, it should always be asynchronous, and so I'd rather see consistency.