Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should JavaScript go inside the <body> for performance, rather than the <head>? [duplicate]

An IBM website talking about rapid web development here mentioned a useful skeleton HTML. In the template, the script inclusion is inside body rather than head. Is it a good practice? Isn't it better to put any library inside head instead?

<html>
  <head>
    <title>Template</title>
    <link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
    <link href="css/bootstrap-responsive.min.css" rel="stylesheet">
  </head>
  <body>
    <!-- The main HTML will go here -->
    <script src="http://code.jquery.com/jquery.js"></script>
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>

VS

<html>
  <head>
    <title>Template</title>
    <link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
    <link href="css/bootstrap-responsive.min.css" rel="stylesheet">
    <script src="http://code.jquery.com/jquery.js"></script>
    <script src="js/bootstrap.min.js"></script>
  </head>
  <body>
    <!-- The main HTML will go here -->
  </body>
</html>
like image 779
Stanley Stein Avatar asked Dec 27 '13 03:12

Stanley Stein


People also ask

Is it better to put JavaScript in head or body?

The best practice is to put JavaScript <script> tags just before the closing </body> tag rather than in the <head> section of your HTML. The reason for this is that HTML loads from top to bottom. The head loads first, then the body, and then everything inside the body.

Should JavaScript be in the head?

You can place the <script> tags, containing your JavaScript, anywhere within your web page, but it is normally recommended that you should keep it within the <head> tag or <body> tag.

Where should I put JavaScript script in HTML?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

What is the difference between JavaScript in head and body?

While embedding JavaScript on a webpage, it may appear almost anywhere within the HTML file. However, the code is usually written either under the head tag or the body tag. Usually, the code written in the head will load before the code written in the body. This is mainly due to the order of the loading.


2 Answers

It is now standard for script tags to be included just before the closing of the body tag so that the script loading does not block the rest of the page loading.

  <script src="myScript.js"></script>
</body>

With this, the user will not have to wait as long to see something on the page, then the javascript functionality is added.

However, there are more and more sites and "web apps" that are javascript/ajax heavy and may require the scripts to be loaded before anything is shown on the page anyway. That is less common, but that is a case where the script could be included in either location, since the visual result would be the same if javascript is responsible for creating/loading the content.

To verify: here is Google's recommendation: https://developers.google.com/apps-script/guides/html/best-practices#load_javascript_last

Also consider loading libraries from a CDN, so that you can take advantage of browser caching.

like image 120
m59 Avatar answered Oct 26 '22 22:10

m59


Yes / No

Yes it is good practice as the page will load faster if the code is in the ending...

It waits for the whole page to load and then loads the scripts.

Here's how:

There was this site i visited, which quickly responded to my browser's request. but when i got the response, it was some white page with some boxes and some text. and it was taking ages for the page to load. After some time some images appeared in the boxes and the text got styled.

Why?

When the page loaded the script was placed in the head. So, the script was loading and the images and the styles were in the queue. So i had to witness an ugly page while the script loads.

Alternative:

If the script was placed in the ending of the page, just before the </body> tag, the styles and images would have loaded faster so i wouldn't have to see an ugly site, then the page would load.

References:

  • http://developer.yahoo.com/performance/rules.html#js_bottom
  • http://stevesouders.com/docs/googleio-20080529.ppt
  • Does Google Analytics have performance overhead?
  • http://www.stevesouders.com/blog/2008/12/27/coupling-async-scripts/
like image 27
Anshu Dwibhashi Avatar answered Oct 26 '22 23:10

Anshu Dwibhashi