Coding with Jesse

addDOMLoadEvent Revisited

August 14th, 2007

I've gone back and reworked addDOMLoadEvent. I got rid of the global variables and reduced the size down to 563 bytes!

For the new script and the demo pages, check out addDOMLoadEvent.

Update [Aug. 19, 2007]: Now the script preserves any existing window.onload function, and also executes functions instantly when called after the page has already loaded. But now the compressed version is a hefty 617 bytes.

Interested in web development? Subscribe to my newsletter!


1 . Ash at 2007-08-17T13:10:45.000Z


Great script, something that the browsers should have supported natively in the first place.

It works well in Firefox, but I'm facing some strange issues in IE.

I tested the new compressed script in IE6 and it triggers a "document.getElementById() is null or not an object" error. The old uncompressed script doesn't return this error.

Also, in both the scripts (old & new), the target function is called twice for some reason, and its not invoking the function after DOM load but rather after the images. So something is broken in IE6 I reckon. Any clues?

Btw, I'm running IE 6.0.2900 on Windows XP SP2.

2 . Ash at 2007-08-17T15:31:17.000Z


Adding to my previous msg: the above problems occur on a page with an iframe (Google Adsense Ad), if that help's diagnosing it.

3 . Jesse Skinner at 2007-08-19T12:23:59.000Z

Jesse Skinner

@Ash - I made some improvements to the script, test it out and see if it things are better. It now preserves the window.onload function. I didn't add the workaround for an iframe with setTimeout, because I'm worried it could create problems in some scenarios (it feels hacky as you admit and I don't know under what circumstances it'll become problematic).

4 . Daniel Hertz at 2007-08-28T16:18:45.000Z

Daniel Hertz

Would it be possible to put the version (revision) numbers and dates in the code and on your blog? That way I know which version of addDOMLoadEvent I'm using.

Thanks for sharing this JS with's very helpful!

Daniel Hertz, Founder