What is the difference when including the <script> tag in the HTML header or body

What is the difference in behavior / performance when a tag is included in the body or head of an html document. I also noticed that google analytics is usually embedded at the end of the body. Why is this?

+7
javascript html
source share
2 answers

In most cases, you want to include scripts at the bottom of the body section. The main reason is to ensure that the page (and therefore the DOM) is fully loaded before any javascript is executed on it.

In addition, since loading scripts prevents the browser from loading anything else at the same time, the page will load faster if the page elements are loaded before the script. However, unless you are a huge website that receives a lot of traffic, such as Yahoo or Google, you probably don't need to consider this.

+13
source share

From Yahoo! Developer Network: Put scripts at the bottom

+9
source share

All Articles