tzador
tzador

Reputation: 2631

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

What is the difference in behavior / performance when including tag in body or head part of html document. Also i noticed that google analytics is usually embedded at the end of body. why is that?

Upvotes: 8

Views: 4855

Answers (2)

Matthew Jones
Matthew Jones

Reputation: 26190

Most of the time you want to include scripts at the bottom of the body section. The basic reason is to ensure that the page (and consequently the DOM) has fully loaded before any javascript is executed upon it.

Additionally, since downloading scripts blocks the browser from downloading anything else at the same time, the page will appear to load faster if the page elements are loaded before the script. However, unless you are a huge website that gets lots of traffic like Yahoo or Google, you probably do not need to consider this.

Upvotes: 13

Crozin
Crozin

Reputation: 44376

From Yahoo! Developers Network: Put Scripts at the Bottom

Upvotes: 9

Related Questions