简体   繁体   中英

jQuery $( function() {} ) and $(document).ready the same?

To have a working datepicker on a field, I have to put this script inside my element

$( function() {
  $( "#date_datepicker" ).datepicker( { dateFormat: "yy-mm-dd" } );
});

Removing the $( function() { makes the datepicker not work.

So does it mean that the $( function() { is the same as $(document).ready ?

I'm trying to optimize my javascript codes so knowing this might help.

See the extract below from http://api.jquery.com/ready/

All three of the following syntaxes are equivalent:

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

The .ready() method is typically used with an anonymous function:

$(document).ready(function() {
  // Handler for .ready() called.
});

Which is equivalent to calling:

$(function() {
 // Handler for .ready() called.
});

As can be read here

Yes, it's a shorthand version of the same thing. The $ function calls the $(document).ready function when passed a function as an argument.

If you're trying to optimise in terms of speed - both will perform pretty much equivalently, however the longer $(document).ready(handler) will be minimally faster if executed lots of times.

If you're trying to optimise in terms of file size - use a minifier.

IMO the best you can do is to 'optimise' in terms of readability and simplicity. This makes the code far easier to understand and maintain. There are tools out there to take an unoptimised version and compress and optimise for you (check out Google's closure compiler).

Yes, $( function() { and $(document).ready are same.

$( function() { works as a shorthand syntax but $(document).ready makes the code more readable.

Here is a pretty safe way to run code on ready

jQuery(function($, undefined){
  // code to run onready
});

Although I personally prefer doing it like this:

(function($){ // create scope and pass specific aliased variables

    $(function($, undefined){ // attach callback to run onready
       // code to run onready
    });

})(jQuery);

This way you can make your own bundles of functionality without fear of breaking other peoples code or having your code broken by loose variable definitions. You can also call the variables that you pass along with whatever names that you want and have code that runs no on ready, for example.

(function($){ // create scope and pass specific aliased variables


    $(document).on('click', 'a[href]', function(){
       // code to run when a link is clicked
    });
    $(window).on('load',function(){
       // code to run onload
    });
    $(function($, undefined){ // attach callback to run onready
       // code to run onready
    });


})(jQuery);

Note that these are the same

$(document).bind('ready', function(){});
$(document).on('ready', function(){});
$(document).ready(function(){});
$(function(){});

And that document does not have a load event

$(document).on('load', function(){}); // will not work

note that you can also find scripts like this:

jQuery(document).ready(function(){

here the $-sign is replace by jQuery to avoid conflicts with other library's

http://docs.jquery.com/Using_jQuery_with_Other_Libraries

Are you using jQuerymobile? if so instead of using document ready use

$('#pageId').live('pageinit',function(){});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM