So I recently bought and downloaded a template from ThemeForest. I set up the template on my local server. I generated the navigation, which has dropdown menus, with codeigniter. I had everything in the menu coded correctly. I only had the header generated and went to test it out and realized the dropdown menus were not working. I spent about two hours trying to figure out what was wrong with my code. Then I finally realized that the JavaScript files were included in the footer of the template... which I had not yet generated.
So my question is... why would someone put JavaScript files in the footer of an HTML page? I thought that JS was only supposed to be located in the header of the file. Right?
Javascript can run every where in your page. not only in header!
But, best for your page speed is put script at bottom (before body closing tag).
Readmore: http://developer.yahoo.com/performance/rules.html#js_bottom
There are different ways you could load a script file to a web page.
But loading scripts at bottom of you page have the following advantages.
$(document).ready(function () {...});
as the DOM is already loaded.Hope this helps.
The big problem with scripts at the bottom is that you cannot have any scripts in the middle of the page (which is a problem with dynamically generated pages) and any page-specific script you want to apply to elements on the page you need to put after loading in your libraries. If, for example you need to load in some ajax content you can only do that after the bottom JS has been loaded. So for many reasons people prefer to have the libraries at least loaded at the top (head) then only put manipulating JS at the foot