DanielAttard
DanielAttard

Reputation: 3615

jQuery Document Ready function syntax

Is there a difference between this:

$(document).ready(function() {

and this:

$().ready(function() {

Upvotes: 0

Views: 667

Answers (4)

Ravi Bhatt
Ravi Bhatt

Reputation: 3163

according to jquery documentation they are the same.

All three of the following syntaxes are equivalent:

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

i personally feel using $(document).ready(handler) makes it more readable though.

Upvotes: 3

bfavaretto
bfavaretto

Reputation: 71938

First, it has nothing to do with PHP, that's javascript code (using the jQuery library). I retagged your question accordingly.

Now, these 3 variants do the same thing (attach an event handler to the DOMLoaded event):

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

The third one is not recommended, according to jQuery docs.

Upvotes: 0

Evenflow
Evenflow

Reputation: 53

If I am not completely mistake, the first one is what you wanna use in any case (when using non-intrusive JS). The second one might even work (not tested) but if it does it will certainly be slower, as jQuery would have to detect the object that is loaded and upon which to run the denoted functon.

Upvotes: 0

ShankarSangoli
ShankarSangoli

Reputation: 69915

They both are equivalent but the later one is not recommended per jQuery docs.

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

Upvotes: 0

Related Questions