Question: For a while I had been running JavaScript component initialization by waiting for the "onload" event to fire and executing a `main()` of sorts. It seemed cleaner and you could be sure the ID state of your DOM was in order. But after some time of putting that through its paces I found that the component's initialization was choked off by any sort of resource hanging during the load (images, css, iframes, flash, etc.).

Now I have moved the initialization invocation to the end of the HTML document itself using inlined `<script />` execution and found that it pushes the initialization before other external resources.

Now I wonder if there are some pitfalls that come with doing that instead of waiting for the "onload".

Which method are you using?

**EDIT**: Thanks. It seems each library has a specialized function for `DOMContentLoaded`/`readyState` implementation differences. I use prototype so [this](http://www.prototypejs.org/api/document/observe) is what I needed.

Answer: 
Jquery has $(document).ready()

The ideal point at which to run most scripts is when the document is ready, and not necessarily when it is "loaded".

[See here](http://encosia.com/2009/03/25/document-ready-and-pageload-are-not-the-same/)
The correct way around the issue would be to use the `DOMContentLoaded` event, which isn't supported in all browsers. Hacks (eg polling `doScroll()` or using `onreadystatechange`) exist, so libraries are able to provide this functionality across browsers.

But there are still problems with `DOMContentLoaded` and chunked transfers which haven't been addressed by popular JavaScript frameworks.

Here's [my take on the issue](http://mercurial.intuxication.org/hg/js-hacks-docs/raw-file/tip/domcontentloaded-en.html).