Reputation: 839
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.
Upvotes: 27
Views: 43280
Reputation: 55683
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
Upvotes: 3
Reputation: 1638
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
Upvotes: 1
Reputation: 23
Are you using jQuerymobile? if so instead of using document ready use
$('#pageId').live('pageinit',function(){});
Upvotes: 0
Reputation: 3095
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)
Upvotes: 44
Reputation: 2577
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.
});
Upvotes: 14
Reputation: 13471
Yes, $( function() {
and $(document).ready
are same.
$( function() {
works as a shorthand syntax but $(document).ready
makes the code more readable.
Upvotes: 3
Reputation: 41767
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).
Upvotes: 5