Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If I keep JavaScript at bottom or keep JavaScript in <head> inside document.ready, are both same thing?

If I keep JavaScript code at bottom or keep JavaScript code in <head> inside document.ready, are both same thing?

I'm confused between these two methodologies, http://api.jquery.com/ready/ and http://developer.yahoo.com/performance/rules.html#js_bottom.

Is there any benefit to putting JavaScript code at bottom (just before </body>) even if I keep the code inside.

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

As JavaScript is attached in

<head>
    <script type="text/javascript" src="example.js"></script>
</head>
like image 258
Jitendra Vyas Avatar asked Oct 13 '10 07:10

Jitendra Vyas


2 Answers

In General, your should put your Javascript files at the bottom of your HTML file.

That is even more important if you're using "classical" <script> tag files. Most browsers (even modern ones) will block the UI thread and therefore the render process of your HTML markup while loading & executing javascript.

That in turn means, if you're loading a decent amount of Javascript at the top of your page, the user will expire a "slow" loading of your page, because he will see your whole markup after all your script has been loaded and executed.

To make this problem even worse, most browsers will not download javascript files in a parallel mode. If you have a something like this:

<script type="javascript" src="/path/file1.js"></script>
<script type="javascript" src="/path/file2.js"></script>
<script type="javascript" src="/path/file3.js"></script>

your browser will

  • load file1.js
  • execute file1.js
  • load file2.js
  • execute file2.js
  • load file3.js
  • execute file3.js

and while doing so, both the UI thread and the rendering process are blocked.

Some browsers like Chrome finally started to load script files in parallel mode which makes that whole problem a little bit less of an issue.

Another way to "workaround" that problem is to use dynamic script tag insertion. Which basically means you only load one javascript file over a <script> tag. This (loader) script then dynamically creates <script> tags and inserts them into your markup. That works asyncronously and is way better (in terms of performance).

like image 145
jAndy Avatar answered Oct 04 '22 15:10

jAndy


They will load all the same in terms of you being able to access the code.

The differences are in the perceived speed of loading of the page. If the javascript is last it will not block any CSS that is trying to be downloaded, which should always be at the top, and will not block any images that need to be downloaded.

Browsers only ask for items as they find them in the HTML but they only have a limited amount of download streams (~10 in modern browsers) so if you doing a lot of requests for images/css and for JS something is going to lose and the perceived speed/ user experience of the page load of your page will take a hit.

like image 35
AutomatedTester Avatar answered Oct 04 '22 15:10

AutomatedTester