views:

115

answers:

7

if i keep javascript at bottom or keep javascript in <head> inside document.ready, are both same thing?

I'm confused between these two methodology

this http://api.jquery.com/ready/ and this http://developer.yahoo.com/performance/rules.html#js_bottom

Is there any benefit to put js at bottom (just before </body>) even I'm keeping the code inside

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

as js is attached in

<head>
<script type="text/javascript" src="example.js"></script>
</head>
+1  A: 

They are not the same thing as the ready event is fired when the DOM tree has been built, while scripts at the end of the page may actually execute afterward.

Either way, they're both safe entry points for your app's execution.

Jacob Relkin
A: 

Position of <script> tag don't involve your script if you use document.ready. It seems JavaScript is charged faster when placed before </body> but I'm not sure.

MatTheCat
+1  A: 

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.

AutomatedTester
Not only *perceived* speed - some old browsers that *just.won't.die* will drop into a different (less parallel) mode for downloading as soon as they encounter a `<script>` tag.
Piskvor
On the other hand, *if* the script is cached, the sooner it appears in the document, the sooner it will be able to start running. This is useless for access to DOM nodes which haven't loaded yet, but can be useful for other purposes, like (half-assed example because I'm tired) changing stylesheet rules for progressive enhancement.
eyelidlessness
+4  A: 

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/file1.js"></script>
<script type="javascript" src="/path/file1.js"></script>

your browser will

  • load file1.js
  • execute file1.js
  • load file2.js
  • execute file2.js
  • load file2.js
  • execute file2.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).

jAndy
So even my code is already inside document.ready , is it better to keep js at bottom?
metal-gear-solid
jAndy
@jAndy - So if my js is already at bottom. then .ready() , will not make any difference.
metal-gear-solid
@metal-gear-solid: interesting point. Actually I would have to answer with "yes" - it should make no difference. The `.ready()` handler should fired instantly when executing a js file at the bottom of your markup. But then again I have second thoughts. Maybe you're loading huge image files in your HTML markup which take longer to load then your js files (those are loaded async by default). But in general, it should make no difference.
jAndy
@jAndy - +1 to you.
metal-gear-solid
+1  A: 

The Yahoo! Developer site is saying that if you put JavaScript at the bottom of the page, it won't block loading of other resources by the browser. This will make the page's initial load quicker.

jQuery is specifying a function to load when the entire page has loaded.

If you have a function which executes on page load, it won't matter whether you include it in <head> or at the bottom of the page, it will be executed at the same time.

Phill Sacre
A: 

Even with the script at the bottom of the HTML document, the DOM may not be fully loaded. All closed elements above the script will typically be ready, a DOM ready event may be necessary in corner cases.

eyelidlessness
+1  A: 

It's important to consider what the JavaScript is actually doing on your page when deciding where to put it. In most cases, the time it takes to load and run JavaScript makes placing it at the end of the page more logical. However, if the page rendering itself depends on Ajax calls or similar, this might not be the case.

Here's a good read on the subject of document.ready() not being appropriate for all JS.

Isaac Lubow