2

Possible Duplicate:
jQuery $( function() {} ) and $(document).ready the same?

Do you know which one is better and why?

The first one;

$(document).ready(function() {
  // your code
});

The second One :

$(function() {
  // your code
});
Community
  • 1
  • 1
tugberk
  • 57,477
  • 67
  • 243
  • 335

4 Answers4

3

It doesn't matter. I'm more of a fan of the 2nd case because its easier to type.

This is what the function does internally.

// HANDLE: $(function)
// Shortcut for document ready
} else if ( jQuery.isFunction( selector ) ) {
   return rootjQuery.ready( selector );
}
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
2

They are equivalent. It depends on how verbose or concise you want to be.

Xaisoft
  • 45,655
  • 87
  • 279
  • 432
1

All three of the following syntaxes are equivalent:

 $(document).ready(handler)
 $().ready(handler) // (this is not recommended)
 $(handler)

http://api.jquery.com/ready/

awe
  • 21,938
  • 6
  • 78
  • 91
heisenberg
  • 9,665
  • 1
  • 30
  • 38
0

Both are the same reference : http://api.jquery.com/ready/

Ahmed Magdy
  • 5,956
  • 8
  • 43
  • 75