I’ve been working with JQuery for some time now and I’ve always used the following to initalise my javascript:
$(document).ready( function() {
// Initalisation logic
});
However, recently I’ve noticed a lot of examples using the following:
$(function() {
});
Whats the difference?
Thanks
Basically, there isn’t one. The
$(...)format is a shortcut. See the API docs forjQuery()for details.I like to use it like this:
…because then if I need to, I can use
noConflictif I end up having to mix something into the page that also wants the$symbol, but I can still use$in my code (because jQuery passes itself into the callback as the first argument, and as you can see I’m accepting that argument as$in my callback function — and so that shadows any global$symbol that another library might be using). The above also has the advantage that I can have symbols global to my code (vars within the anonymous function) that are not actually globals.