views:

307

answers:

4

I know that, in jQuery, we are told to use $(document).ready() in order to ensure that the DOM elements are ready for interaction. I know that this definitely applies if the script tags are in the <head>. If they are at the end of the <body>, after all of the DOM elements, should I still use a DOM-ready function? Are there browsers in which my code would fail if I did not?

Thanks!

A: 

The DOM-ready function ensures that the entire DOM has actually been created so that all elements in the document can be reached. The normal window.onload loads only when all images have been loaded, while jQuery's event triggers as soon as the document structure exists. If you need something to happen "right when the document loads", there is no reason to use anything but $(document).ready().

Christian P.
So my question is, is the document sufficiently loaded by the time my scripts execute?
Matchu
A: 

I have never encountered a browser where you could not access the DOM of html elements appearing before my script.

That said, if you are pulling in jQuery or so already, I would probably use the ready() method, just to make it easier for other people used to jQuery to understand the code.

Is there some reason why you would want to avoid this it?

(If you are looking for a way to avoid loading jQuery, then, well, yeah, in my experience, you can always access the DOM of elements appearing before the script.)

Roland Bouman
I already am using jQuery for general DOM access, but my script is organized in such a way that, in order to correctly modularize certain DOM-ready actions, they end up in different parts of the code. It'd look cleaner if I could just remove the $(function () {}), but I'm not willing to do so until I'm sure that removing it can't possibly hurt anything.
Matchu
+1  A: 

There is another reason for putting your scripts at the end: browsers do not load JS files in parallel because they can affect everything that comes after. In fact, everything stops in the browser until the JS file is downloaded and parsed -- text, images, everything. So unless you have to load them early, load your JS at the end of the page.

Peter Rowell
Annie
@Annie: Thanks for the link. I hadn't realized they had fixed the parallel-script-download problem in FF 3.5 and MSIE 8. It's interesting to note they both will download script/script and script/stylesheet in parallel, but not script/image. Any idea why that might be?
Peter Rowell
+5  A: 

There is one thing you can't do in a <script> block just before </body>: append DOM content to the body. This is the append-relative-to-parse problem that causes IE to throw a fit with the dreaded “Operation aborted”.

So if you have scripts or plugins that do that, you can't invoke them inline at the end of the body element. Otherwise go ahead.

It won't get you anything on up-to-date Mozilla, Opera or WebKit browsers since those will fire ready in a moment anyway. It will avoid an unpleasant but largely harmless hack loop in IE, and it'll fire much sooner for other (older or more obscure) browsers that otherwise fall back to waiting for onload.

bobince
+1 Beat me to it, all great points!
Josh Stodola
Good to know - thanks!
Matchu
No, IE allows manipulation of the containing parent of the `<script>` element. So if the `<script>` tag is before the closing </body>, it can append content to the body fine.
Crescent Fresh