Last Updated: February 24, 2021
·
13.36K
· pascal

jQuery initialization

you can also use:

$(function() {

});

instead of

$(document).ready(function() {

});    

when you initialize jQuery.

Don't us the following example cause the DOM is not yet ready when it runs.

(function(){

})(jQuery)

1 Response
Add your response

that last thing (let's call this closure an "anonymous scope") is totally valid given it is put in the right place. this construct is also considered best practice in javascript world to encapsulate your code from the global scope.

if you run it at the end of the page, you should not have any problems.
Also, the closure lacks an argument variable to hold the jquery reference you passed in (although it would work anyway :p).
You can even combine this with .noConflict() and use $ inside the closure at the same time:

(function($){
  // code
})(jQuery.noConflict())
over 1 year ago ·