3

I'm new to jquery. I've just read that these 2 are equivalent:

$(document).ready(function() {});

$(function() {});

Which one is better practice, or more accepted? The first one strikes me as clearer in that it states the "document.ready" part - but then again, I'm new to jquery. It could be that to anyone with any experience in jquery, the second option just as clearly implies "document.ready". Which of these options should I choose?

froadie
  • 79,995
  • 75
  • 166
  • 235

2 Answers2

4

Feel free to use either one of them. Just as you think, the latter version is a shorthand for $(document).ready().

The first option was available since the beginning of jQuery releases as oppose to the second option.

Personally I prefer the second version for it is shorter.

Omar
  • 32,302
  • 9
  • 69
  • 112
WTK
  • 16,583
  • 6
  • 35
  • 45
1

All three of the following syntaxes are equivalent:

$(document).ready(handler);

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

$(handler);
Robin Maben
  • 22,194
  • 16
  • 64
  • 99
  • I understand that. I'm just asking if there's any reason you would choose one over the other, considering that they're identical – froadie Sep 05 '11 at 12:00
  • That would depend a lot on what you're doing within `handler()`. Sometimes, there are clear disadvantages of using `ready()` vs not using it. – Robin Maben Sep 05 '11 at 12:14