I had a thought to do something like this:
(function(window, undefined){
$ = window.jQuery;
$(function(){
// Do some stuff
});
})(this);
Would you consider this good practice or bad? Does it have any implications for when jQuery(document).ready() would fire?
Only reason I'd say would be if you have some javascript to run before the DOM is ready, and you don't want to pollute the global namespace.
(function(window, undefined){
var $ = window.jQuery;
// create some variables and/or functions that shouldn't be global
// ...and do some work before the "ready()" fires
var a = 'some value';
function b() {
// do some important stuff
}
var c = b();
// Maybe set up a `.live()` handler, which doesn't rely on DOM ready.
$('.someSelector').live( function() {
// Some handler code.
// This works before DOM is ready.
});
$(function(){
// Your DOM ready code
});
})(this);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With