Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery: Choosing a document.ready method

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?

like image 992
froadie Avatar asked Jun 22 '26 12:06

froadie


2 Answers

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.

like image 70
WTK Avatar answered Jun 24 '26 11:06

WTK


All three of the following syntaxes are equivalent:

$(document).ready(handler);

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

$(handler);
like image 24
Robin Maben Avatar answered Jun 24 '26 12:06

Robin Maben



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!