Multiple $(document).ready()
$(document).ready() that I didn't mention in my previous post is that you can use it more than once. In fact, if you don't care at all about keeping your code small, you could litter your javascript file with them.
It's great to be able to group your functions within a file or even across multiple files, and jQuery's flexible $(document).ready() function allows you to do that, pain free.
You could, for example, have one .js file that is loaded on every page, and another one that is loaded only on the homepage, both of which would call $(document).ready(). So, inside the <head> tag of your homepage, you would have three references to JavaScript files altogether, like so:
- <script src="/js/jquery.js" type="text/javascript"></script>
- <script src="/js/common.js" type="text/javascript"></script>
- <script src="/js/homepage.js" type="text/javascript"></script>
You could also do something like this inside a single .js file:
A final note: In a comment to my previous post, Jörn gave this excellent tip for shrinking your code:
Coming Up: In my next entry, I'll show how to do a simple effect with jQuery. You'll be amazed at how easy it is!Even for this little amount of code is a shortcut available:
A function passed as an argument to the jQuery constructor is bound to the document ready event.$(function() { // do something on document ready });














