Any difference between these two statements?
$(document).ready(function() {
// Code
});
$(function() {
// Code
});
Any difference between these two statements?
$(document).ready(function() {
// Code
});
$(function() {
// Code
});
Did you mean:
$(function ()
{
/* ... */
});
They are equivalent in jQuery. The latter is shorthand for the former.
If you mean
$(document).ready(function() {/* code here */});
and
$(function() {/* code here */});
then there is no difference between the two. The latter is just a shortcut that does the same thing as the former.
For all intents and purposes these two statements do the same thing. Internally however the second one will call the first one.
In resolution, there is no difference, they are equivalent
From the relevant source code
jQuery = window.jQuery = window.$ = function( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init( selector, context );
}
jQuery.fn = jQuery.prototype = {
init: function( selector, context ) {
/* .... irrelevant code.... */
// HANDLE: $(function)
// Shortcut for document ready
else if ( jQuery.isFunction( selector ) )
return jQuery( document ).ready( selector );
}
}
I think it's important to point out that the official jQuery line is that the second syntax:
$(function() {
// code here
});
is preferred. Why? Because it provides for the possibility that Mr. Resig & Co. will come up with a brilliant new way to perform on-load initialization that's way better than using the "ready" event. If they do that, and you use the simpler (second) way of doing things, then your code will immediately benefit from the improvement without any need to change anything. If you use the explicit binding to the "ready" event, you're sticking with that approach until you change it.