Is "Put Scripts at the Bottom" Correct? [closed]
Solution 1:
When a user requests a page from your site, the page HTML starts streaming to the browser. As soon as a browser encounters a tag for an external image, script, CSS file, etc., it will start downloading that file simultaneously.
If you put your scripts at the bottom of a page, they'll be loaded last. In other words, the HTML content/structure/css/images of youe page/app will be loaded first, and something can show up in the browser faster; Your users don't have to wait for a script to finish downloading before they see something in your application.
Solution 2:
The reason why pages that have scripts at the top of the page still score an 'A' is because this is not as important as other performance improvements that could be made.
Each rule is weighted, so some rules affect the ySlow grade more than others.
I always put scripts at the bottom. There are very few reasons for needing scripts at the top of your page. The only reason I can think of is you need your JavaScript to execute immediately before anything else in the page, which is quite rare.
Solution 3:
Sometimes, you have no choice but to put scripts at a specific location in page. However, if you put scripts at the bottom of the page, the browser won't interrupt its rendering to load JS engine to process your script (which might be costly, if you have loops running for large number of times) and will probably display an early view of the page sooner.
Solution 4:
The code may reference DOM objects that haven't been instantiated yet is the most obvious reason I can think of.