Dirty Bird Design
Dirty Bird Design

Reputation: 5533

In jQuery, is $(func) equivalent to $(document).ready(func)?

Is

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

the same as

$(function(){});

?

I believe it is, actually I'm 99% sure it is but wanted a 'second' opinion

Upvotes: 11

Views: 4358

Answers (3)

chainwork
chainwork

Reputation: 2886

Yes

"the document-ready idiom is so common, in fact that there's a shortcut version of it... The expanded version $(document).ready is arguably a better example of self-documenting code; it's much easier to see at a glance exactly what's going on - especially if it's buried in a page of another developers JavaScript!" - Borrowed from - jQuery Novice to Ninja

Upvotes: 5

jessegavin
jessegavin

Reputation: 75650

Yes it is the same.

From the jQuery Docs - http://api.jquery.com/ready/

All three of the following syntaxes are equivalent:

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

Upvotes: 11

rfunduk
rfunduk

Reputation: 30442

Indeed it is. Personal preference.

Upvotes: 1

Related Questions