0plus1
0plus1

Reputation: 4555

Js, is it really better to include at the end of the page?

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

Upvotes: 1

Views: 104

Answers (3)

Guffa
Guffa

Reputation: 700572

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.

Upvotes: 0

Jim Lamb
Jim Lamb

Reputation: 25775

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.

Upvotes: 0

Pointy
Pointy

Reputation: 413826

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.

Upvotes: 2

Related Questions