How does Jquery domready work?
I'm looking at jQuery source code for tracking the domready event and there's one thing I don't understand completely. What if the script is included dynamically and is loaded asynchronously? That way the document might already be loaded when the script loads. The only place jQuery checks for this is the document.readyState.
Correct me if I'm wrong, but hasn't this property been introduced to Firefox just very recently? If so - the DomContentLoaded event might have already been fired开发者_高级运维 (even window's onLoad might) and there is no way to actually call the code on domready in such situation.
In the current version of jQuery, it's all handled with the "Deferred" mechanism. The readiness of the page is encapsulated in a Deferred object, and functions are simply added to the "done()" queue. The Deferred API simply remembers whether the page is ready or not, so passing in a function as a "ready" handler after the page is ready will simply call the function immediately.
The code to keep track of when "ready" actually happens is pretty tortured, however.
精彩评论