When embedding JavaScript in an HTML document, where is the proper place to put the <script>
tags and included JavaScript? I seem to recall that you are not supposed to place these in the <head>
section, but placing at the beginning of the <body>
section is bad, too, since the JavaScript will have to be parsed before the page is rendered completely (or something like that). This seems to leave the end of the <body>
section as a logical place for <script>
tags.
So, where is the right place to put the <script>
tags?
(This question references this question, in which it was suggested that JavaScript function calls should be moved from <a>
tags to <script>
tags. I'm specifically using jQuery, but more general answers are also appropriate.)
The standard advice, promoted by the Yahoo! Exceptional Performance team, is to put the
<script>
tags at the end of the document body so they don't block rendering of the page.But there are some newer approaches that offer better performance, as described in this answer about the load time of the Google Analytics JavaScript file:
Depending on the script and its usage the best possible (in terms of page load and rendering time) may be to not use a conventional <script>-tag per se, but to dynamically trigger the loading of the script asynchronously.
There are some different techniques, but the most straight forward is to use document.createElement("script") when the window.onload event is triggered. Then the script is loaded first when the page itself has rendered, thus not impacting the time the user has to wait for the page to appear.
This naturally requires that the script itself is not needed for the rendering of the page.
For more information, see the post Coupling async scripts by Steve Souders (creator of YSlow but now at Google).
If you are using JQuery then put the javascript wherever you find it best and use
$(document).ready()
to ensure that things are loaded properly before executing any functions.On a side note: I like all my script tags in the
<head>
section as that seems to be the cleanest place.script tag should be use always before body close or Bottom in HTML file.
then you can see content of the page first before loading js file.
check this if require : http://stevesouders.com/hpws/rule-js-bottom.php
Script blocks DOM load untill it's loaded and executed.
If you place scripts at the end of
<body>
all of DOM has chance to load and render (page will "display" faster).<script>
will have access to all of those DOM elements.In other hand placing it after
<body>
start or above will execute script (where there's still no DOM elements).You are including jQuery which means you can place it wherever you wish and use .ready()
Non-blocking script tags can be placed just about anywhere:
async
script will be executed asynchronously as soon as it is availabledefer
script is executed when the document has finished parsingasync defer
script falls back to the defer behavior if async is not supportedSuch scripts will be executed asynchronously/after document ready, which means you cannot do this:
Or this:
Or this:
Or this:
Having said that, asynchronous scripts offer these advantages:
Browser can download stylesheets, images and other scripts in parallel without waiting for a script to download and execute.
You can place the scripts inside head or body without worrying about blocking (useful if you are using a CMS). Execution order still matters though.
It is possible to circumvent the execution order issues by using external scripts that support callbacks. Many third party JavaScript APIs now support non-blocking execution. Here is an example of loading the Google Maps API asynchronously.