The docs jQuery API site for ready
All three of the following syntaxes are equivalent:
- $ (document) .ready (handler)
- $ (). ready (handler) (this is not recommended)
- $ (handler)
After doing homework - reading and playing with the source code , I have no idea why
$().ready(handler)
Not recommended. The first and third methods, exactly the same, the third option calls a ready-made function to the cached jQuery object using document :
rootjQuery = jQuery(document); ... ... // HANDLE: $(function) // Shortcut for document ready } else if ( jQuery.isFunction( selector ) ) { return rootjQuery.ready( selector ); }
But the ready function has no interaction with the selector of the selected node elements, the source code is ready :
ready: function( fn ) {
As you can see, it simply adds a callback to the internal queue ( readyList ) and does not change or use the elements in the set. This allows you to call the ready function on each jQuery object.
how
- regular selector:
$('a').ready(handler) DEMO - Nonsense series :
$('fdhjhjkdafdsjkjriohfjdnfj').ready(handler) DEMO - Undefined :
$().ready(handler) DEMO
Finally ... to my question: Why is $().ready(handler) not recommended?
javascript jquery callback document-ready
gdoron May 25 '12 at 11:03 2012-05-25 11:03
source share