Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use DOM-ready functions if my scripts are at the end of the body?

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!

like image 950
Matchu Avatar asked Jan 03 '10 03:01

Matchu


1 Answers

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.

like image 174
bobince Avatar answered Oct 15 '22 12:10

bobince