views:

54

answers:

3

Currently I'm doing this.

<js>
</head>

Pagespeed and other articles suggest doing this instead.

<js>
</body>
</html>

I would love the opinion of stack overflow!

Thank you

+2  A: 

The only issue with putting all your scripts at the end of the body is that sometimes page components drop little bits of Javascript on the page that may assume the existence of some Javascript facility. Other than that, there's nothing wrong with it and it can help make your pages appear to load/render faster.

You might also look into tools like LabJS (http://labjs.com) as a more sophisticated way to load your Javascript.

Pointy
A: 

Use progressive enhancement so that your page will work whether JavaScript is enabled or not. Then, move the Javascript to the bottom of the page so that your page's content loads and renders first. And, as always, test the performance of your page using Page Speed or YSlow.

Jim Lamb
A: 

The default way of including Javascript is to do it from the head tag, that's where they normally belong. That's where you should start out, and only move the scripts if you need to optimise the loading of the page.

There are some reasons to put some scripts later in the page:

  • To make the content of the page load earlier
  • To make the page less sensetive to slow loading external scripts

There are some special considerations if you move scripts down the page:

  • Some scripts doesn't work if the load after the content.
  • You should make sure that the content doesn't look bad or does anything nasty if the user tries to use it before the scripts are loaded.
Guffa