By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,397 Members | 1,080 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,397 IT Pros & Developers. It's quick & easy.

Script tags in header or end of body tag?

P: 1
Where should i put my javascript file calls?

Inside the head tag or at the end of the body tag?

Are there benefits for choosing one over the other?
4 Weeks Ago #1
Share this Question
Share on Google+
3 Replies

SwissProgrammer
100+
P: 127
Javascript file calls should be placed only as close to the first part of your HTML as is needed for speed of loading the page and for speed of the user getting locally useful information from the page.

Example: If your page has a lot of text that is of far more importance to the user than the effects of your Javascript, then place the Javascript further down the page to allow the text to show up quicker.

Example: If your user might have intermittent or unstable or slow speed downloads, and if your Javascript is simply to make trailing sparkles behind the mouse, and if your user might have to wait a long time to get the actual page to load, then put that Javascript near or at the bottom of the HTML of the page.

Example: If your user is looking for a Virtual Reality rendering which has user interaction capacity like WRL has, and if the text and other parts of the page are less important, then put the Javascript that creates the VR near to the top of the page.

You have to determine the placement of the Javascript based upon the needs of the user of the page (within reason).

There are a lot of pages that are full of what the author thinks is impressive, but that irritate the user.

Think about each application and do what is needed.

.
4 Weeks Ago #2

dev7060
Expert 100+
P: 335
Where should i put my javascript file calls?

Inside the head tag or at the end of the body tag?

Are there benefits for choosing one over the other?
Depends. If the script is interacting with the DOM, you have to place it before the closing of the </body>, otherwise, all DOM objects would hold null.
3 Weeks Ago #3

P: 70
Hey

The best practice is to put the <script> tags at the end of the document body so they don't block rendering of the page. But If there are active components on your page which are using external scripts,
then their dependency (js files) should come before that (ideally in head tag).
3 Weeks Ago #4

Post your reply

Sign in to post your reply or Sign up for a free account.