JavaScript at bottom/top of web page?

JavascriptWebpage

Javascript Problem Overview


I was just using the plugin "Yslow" for Mozilla Firefox, and it told me that I should put JavaScript at the bottom. I have heard this before but haven't really thought about it too much. Is there really an advantage in putting JavaScript at the bottom of a web page compared to the top?

Javascript Solutions


Solution 1 - Javascript

It'll allow the web page to load visibly before executing JavaScript, which makes sense for things like Google Analytics, which don't need to happen before the page loads.

You may also want to look into things like jQuery, prototype, etc and attach to the "ready" handler, which executes JavaScript code after the DOM has been fully loaded, which is an appropriate place for much JavaScript code.

Solution 2 - Javascript

Assuming you aren't running on a CDN or aren't serving your JS from a separate sub-domain or server, it will load synchronously and force your HTML content to wait until it has downloaded the files. By placing the JS at the bottom of your page before the closing </body> tag, you are allowing the HTML to be parsed prior to loading the javascript. This gives the effect of faster page load times.

Solution 3 - Javascript

If you have static html content and a lot of javascript, it can make a difference in perceived page load time since the html will load first giving the user something to look at. If you don't have much javascript, or the existing page content relies on the javascript to be useful, then this is not as useful practically-speaking.

Solution 4 - Javascript

Yes, the page will load the content and render it before loading and executing javascript, and the page will, as a result, load faster.

Solution 5 - Javascript

I want to bring update to this topic, google has recently introduced async snipped http://support.google.com/analytics/bin/answer.py?hl=en&answer=1008080&rd=1 which can be added for your site to bring e.g. google statistics support. It should be placed bottom of the <head> section for best performance. The point is that this increases likely hood of tracking beacon to be sent before user leaves the page.

Also it should be located there if you want to verify your site in google webmaster tools using your google analytics.

Other than that, same rules still applies basically - javascript at bottom for "fast" loading of the page. I used quotes because I dont count page fully loaded until javascript finishes ;-)

Solution 6 - Javascript

TOP

> When you put your JavaScript at the top of the page, the browser will start loading your JS files before the markup, images and text. And since browsers load JavaScript synchronously, nothing else will load while the JavaScript is loading. So there will be a timeframe of a few seconds where the user will see a blank page, while the JavaScript is loading.

BOTTOM

> On the other hand, if you place your JavaScript at the bottom of the page, the user will see the page loading first, and after that the JavaScript will load in the background. So if, for example, your CSS & HTML takes 5 seconds to load, and your JavaScript takes another 5 seconds, putting our JavaScript on the top of the page will give the user a “perceived” loading time of 10 seconds, and putting it on the bottom will give a “perceived” loading time of 5 seconds.

Taken from Demian Labs.

Solution 7 - Javascript

It allows all the DOM elements to fully load before loading the Javascript which addresses them. This standard is also part of Visual Studio.

Solution 8 - Javascript

Placing scripts at the bottom of the element improves the display speed, because script compilation slows down the display.

Solution 9 - Javascript

Yes including the javascript at the bottom of the page really quickens the loading of the page. Since browser executes things synchronously it impacts the page loading if it is placed at the top of the page. If it is placed at the bottom of the page, the page would have loaded the entire markup by then when the browser starts loading the javascript giving a better experience to the user.

Solution 10 - Javascript

It's advisable to put all inline scripts at the end to improve performance, you don't want your users to be staring at a blank white screen while the script renders. You can use defer attribute eg.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionuserView Question on Stackoverflow
Solution 1 - JavascriptwojoView Answer on Stackoverflow
Solution 2 - JavascriptCorey BallouView Answer on Stackoverflow
Solution 3 - JavascriptBrian MoeskauView Answer on Stackoverflow
Solution 4 - JavascriptCodeJoustView Answer on Stackoverflow
Solution 5 - JavascriptMauno VähäView Answer on Stackoverflow
Solution 6 - JavascriptivanleonczView Answer on Stackoverflow
Solution 7 - JavascriptUmar AlFarooqView Answer on Stackoverflow
Solution 8 - JavascriptAakriti KishoreView Answer on Stackoverflow
Solution 9 - Javascriptraj240View Answer on Stackoverflow
Solution 10 - JavascriptDexterView Answer on Stackoverflow