Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript in <head> or just before </body>?

I am about to embark on a new web project and I plan to put some JavaScripts in the <head> and also some before </body>, using the following scheme:

  1. Scripts that are essential for the UX of the page: in the <head>. As I've picked up perusing the web - scripts in the <head> is loaded before the page loads, so it would make sense to put scripts that are essential to the user experience there.

  2. Scripts that are non-essential to the design and UX (Google Analytics scripts etc.): before the </body>.

Is this a sensible approach?

Another approach would be to put all the scripts in the <head> and add defer attributes to the non-essential scripts. However, I read that older versions of Firefox don't pick up the defer attribute.

like image 994
timkl Avatar asked Oct 13 '11 08:10

timkl


People also ask

Should I put js script 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.

Does body or head come first in HTML?

head is required and it should be used just once. It should start immediately after the opening html tag and end directly before the opening body tag.

Is it better to place the JavaScript code at the beginning or the end of the webpage?

The script tag should always be used before the body close or at the bottom in HTML file. The Page will load with HTML and CSS and later JavaScript will load.

What is the difference when the script tag appears in the HTML body or head sections?

When you place a script in the head section, you will ensure that the script is loaded before anyone uses it. Scripts in the body section: Scripts to be executed when the page loads go in the body section. When you place a script in the body section it generates the content of the page.


2 Answers

I think a lot of developers run JavaScript just before the </body> so that it is run after all the elements have been rendered.

However, if you organise your code correctly, the position on the page doesn't matter.

For example, when using jQuery, you can ensure the code isn't run until the page and its elements are fully rendered by doing the following:

$(document).ready(function(){    //Code here }); 

Then the script reference can be put in the head tag.


Script tags should be referenced just before </body>. This prevents render blocking while the scripts load and is much better for site perception speed.

No obtrusive JavaScript should be used when using this technique.

like image 121
Curtis Avatar answered Sep 18 '22 03:09

Curtis


JavaScript code should be placed at the end of the document so that it doesn't delay the parallel loading of page elements. This does then require that the JavaScript code is written in a specific way, but it does improve the speed of page loads.

Also, ideally you could host references like this under a different (sub)domain. References to jQuery should be pointed to Google's CDN too.

See Best Practices for Speeding Up Your Web Site for more information.

like image 29
Digbyswift Avatar answered Sep 20 '22 03:09

Digbyswift