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.


Comments

1 . Ash on August 17th, 2007

Ash

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 on August 17th, 2007

Ash

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 on August 19th, 2007

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 on August 28th, 2007

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 us...it's very helpful!

Daniel Hertz, Founder
Pixody.com

Comments are closed, but I'd still love to hear your thoughts.