Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

Is

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

the same as

$(function(){});

?

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

like image 316
Dirty Bird Design Avatar asked Sep 07 '10 20:09

Dirty Bird Design


People also ask

Which one is equivalent to $( document .ready function?

Answer: Use the DOMContentLoaded Eventready() equivalent without jQuery.

Is DOMContentLoaded the same as document ready?

ready() method differs in an important and useful way: If the DOM becomes ready and the browser fires DOMContentLoaded before the code calls . ready( handler ) , the function handler will still be executed. In contrast, a DOMContentLoaded event listener added after the event fires is never executed.

What is .ready in jQuery?

jQuery | ready() with Examples The ready() method is an inbuilt method in jQuery which helps to load the whole page then execute the rest code. This method specify the function to execute when the DOM is fully loaded. Syntax: $(document).ready(function)

What is the difference between $( window .load and $( document .ready function in jQuery?

The key difference between $(document). ready() and $(window). load() event is that the code included inside onload function will run once the entire page(images, iframes, stylesheets,etc) are loaded whereas the $(document). ready() event fires before all images,iframes etc.


2 Answers

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)

like image 94
jessegavin Avatar answered Sep 22 '22 12:09

jessegavin


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

like image 38
chainwork Avatar answered Sep 19 '22 12:09

chainwork