Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between these jQuery ready functions?

Tags:

jquery

what is difference between

$(function(){  });  

and

$(document).ready(function() {   }); 
like image 947
Starx Avatar asked Apr 18 '10 15:04

Starx


People also ask

What is difference between $( function () and document Ready?

So technically they are both the same. Not major difference between these two declaration. They used based on weather you use JavaScript then you should use $(document). ready declaration in other case you use jQuery library which is a part of JavaScript then you should use $(function) declaration.

What is ready function in jQuery?

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) Parameters: This method accepts single parameter function which is mandatory.

What is the difference between $( Windows .load & $( document .ready function in jQuery?

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. are loaded, but after the whole DOM itself is ready.

What is the difference between using $( function and function?

Functional is different from function. A function is a mathematical machine which accepts one or more numbers as inputs and provides a number as an output. A functional is that accepts one or more functions as inputs and produces a number as an output. So, a Functional is a function of Functions.


2 Answers

Nothing whatsoever.

This function behaves just like $(document).ready(), in that it should be used to wrap other $()

You can see this in the source code:

rootjQuery = jQuery(document);  ...  } else if ( jQuery.isFunction( selector ) ) {     return rootjQuery.ready( selector ); } 
like image 189
SLaks Avatar answered Oct 12 '22 12:10

SLaks


} else if (jQuery.isFunction(selector)) {     return rootjQuery.ready(selector); } 

From the source

Calling $(document).ready(selector) saves a few if statements.

Although jQuery does cache $(document) internally that might make $(f) faster.

Benchmarked

like image 28
Raynos Avatar answered Oct 12 '22 14:10

Raynos