Thursday, May 2, 2013

Javascript - Script tag in head or bottom of HTML

There are two possibilities for truly unobtrusive scripts:
  • including an external script file via a script tag in the head section
  • including an external script file via a script tag at the bottom of the body (before </body></html>)
The second one can be faster as the original Yahoo research showed some browsers try to load script files when they hit the script tag and therefore don't load the rest of the page until they have finished. However, if your script has a 'ready' portion which must execute as soon as the DOM is ready you may need to have it in the head. Another issue is layout - if your script is going to change the page layout you want it loaded as early as possible so your page does not spend a long time redrawing itself in front of your users.
If the external script site is on another domain (like external widgets) it may be worth putting it at the bottom to avoid it delaying loading of the page.
And for any performance issues do your own benchmarks - what may be true at one time when a study is done might change with your own local setup or changes in browsers.

Reference: http://stackoverflow.com/questions/143486/unobtrusive-javascript-script-at-the-top-or-the-bottom-of-the-html-code 

Related Posts:

  • Node.jshttp://nodejs.org Cannot find module Error cmd>npm install <name_of_module> ie: ejs, express, formidable Setup App cmd>node server.js… Read More
  • Javascript - Generate Documentation for Javascript Case 1 - using Javadoc http://usejsdoc.org/about-commandline.html cmd>jsdoc src -r -t haruki -c /path/to/my/conf.json -d docs   The above will generate documentation for all relevant files found in the sr… Read More
  • Joomla - How to easily add Javascript snippets If you're into analyzing the traffic of your Joomla website (as you should be IMHO), you will probably need to add some tracking software to your site. The most obvious one is Google Analytics. Other choices… Read More
  • Collections, Objects and Arrays in JavaScript - how to loop through a collection of objects and their properties - how to check whether an object is null or undefined - how to find a particular object with a search in a collection or an array - and so on… It… Read More
  • Create GUID in JavaScript Case 1: jquery.guid.js var guid = jQuery.Guid.New() but sometimes, it return an error Guid is undefined, etc ... Case 2:  function S4() {     return (((1+Math.random())*0x10000)|0).toStr… Read More

0 comments:

Post a Comment