what is difference between
$(function(){
});
and
$(document).ready(function() {
});
I use $(function() {});
because it's shorter. As far as I know there is no difference between the two ways of doing it.
Nothing whatsoever.
This function behaves just like $(document).ready(), in that it should be used to wrap other $()
You can see this in the source code:
rootjQuery = jQuery(document);
...
} else if ( jQuery.isFunction( selector ) ) {
return rootjQuery.ready( selector );
}
The two are exactly equivalent: use whichever form you like.
That said, I personally always use the expanded form $(document).ready(function(){});
for the simple reason that it is completely obvious what the code is doing. The approximate idea is that of "self-documenting code". Anyone coming to the code later on will immediately see that the code is to be run on the document
's ready
event. With the short-hand form, you have to rely upon the reader of your code understanding the meaning.