hey guys,
i found a very intersting website template that has in it’s header file the following lines:
(function($){
$(function(){
…normal jquery
}); // end of document ready
})(jQuery); // end of jQuery name space
what does that mean? i thought if i want to use domready i hav to write:
$(document).ready(function(){
where is the difference and can i change that?
thank you for your help
$(function() {is a just a shortcut for$(document).ready(function() {, they are equivalent. The(function($){….})(jQuery);portion is just for compatibility, so that$is thejQueryobject inside the scope (even if it’s something else outside).Something to note here:
document.readyhandlers receivejQueryas their first argument, so the site’s code could be more concisely expressed as:So the answer your question: do you have to change it? nope, use whichever format you prefer…I prefer
$(function() { });because it’s less to type and I do it a hundred times a day. If you find$(document).ready(function() { });much clearer, stick with that, but they behave the same…so use whichever is clearer to you and your team.