Is $(document).ready necessary?

Joshua Robison picture Joshua Robison · Jan 10, 2011 · Viewed 57.1k times · Source

I saw this question in stackoverflow but do not feel that it was answered at all.

Is $(document).ready necessary?

I link all my javascripts at the bottom of the page so in theory they are all running after the document is ready anyways.

Answer

zzzzBov picture zzzzBov · Jan 10, 2011

Is $(document).ready necessary?

no

if you've placed all your scripts right before the </body> closing tag, you've done the exact same thing.

Additionally, if the script doesn't need to access the DOM, it won't matter where it's loaded beyond possible dependencies on other scripts.

For many CMS's, you don't have much choice of where the scripts get loaded, so it's good form for modular code to use the document.ready event. Do you really want to go back and debug old code if you reuse it elsewhere?

off-topic:

As a side note: you should use jQuery(function($){...}); instead of $(document).ready(function(){...}); as it forces the alias to $.