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!
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With