Possible Duplicate:
What is the difference between these jQuery ready functions?
I have always used:
$(document).ready(function (){
//Code goes here
});
And inserted my jQuery/JavaScript code therein (so that it waits on the html page to be fully loaded before running any code).
Lately, I have seen this:
jQuery(function($){
//Code goes here
});
I have searched for what the difference is here (mainly what the ‘jQuery’ part is all about).
My questions:
- What does the
jQuerypart of the latter code block do, if anything? - What is the ‘($)’ argument doing for that function?
- Is there even a difference (other than making the page wait to be loaded) between my two code block examples?
The main difference is the closure around the
$variable. The first snippet assumes that the$variable has not been released (by using .noConflict() or if another library you are using also makes use of$but not as the jQuery variable).The second implementation is safer because it doesn’t make any assumptions but allows your internal code to safely use the
$variable by making it a parameter.