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)
Written by pascal
Related protips
1 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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Javascript
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#