Forum Moderators: open

Message Too Old, No Replies

Call Javascript files in Header or Footer?

Javascript, Javascript call in Header or Footer

         

cyberpunkstudio

11:04 am on Sep 14, 2010 (gmt 0)

10+ Year Member



I've been using JavaScript for a few years now I'm not a huge expert I rely more of JQuery. I am just wondering what best solutions are there to call Javascript files (and to make pages load faster):

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script src="_js/jquery.validate.js" type="text/javascript"></script>
<script src="_js/jquery.metadata.js" type="text/javascript"></script>
<script src="_js/script.js" type="text/javascript"></script>

Normally I request all these in the Header like you normally would but I have seen and heard people to call their Javascript files in the footer to make page load faster and have all their Javascript functions all in the footer as well.

Is this a good idea? What are the downside? Should I just keep all my JS in the Header as usual?

Frank_Rizzo

1:08 pm on Sep 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Best to place them in the footer unless something specifically needs to be in the header. I would also recommend:

1. Don't download from googleapis. Keep it on your own server where you can update it / modify as needed. Some may disagree with this.
2. Make sure all files are minified / compressed. Don't forget to keep offline master files.
3. Merge all files into one big .js file.

Fotiman

1:31 pm on Sep 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



<script> elements should be placed as late in the body as possible.

While JavaScript files are being downloaded and processed, the browser blocks all other resources from being downloaded. Placing the scripts at the end of the file allow the browser to download other resources in parallel, thereby giving a more responsive feeling experience to the end user.

The best place for <script> elements is just before the closing </body> tag.

cyberpunkstudio

1:36 pm on Sep 14, 2010 (gmt 0)

10+ Year Member



So everyone's opinion is to place it just before the close </body> tag. I guess it makes sense to download all the other resources and run <script> latest.