So between John Resig over at jQuery, Dean Edwards, and Matthias Miller, there's been a lot of work being done to get a better implementation of a DOM document loaded script working. And they've got it working for just about every major modern browser.
This is very cool. What does this mean? It means that you don't have to rely on window.onload anymore. What's the problem with window.onload? You have to wait until every major binary loads and everything, but what you really want is just to fire off your scripts when the Document Object Model (DOM) is complete. Then, you can add and remove, hide and show anything that still might be coming down the pipe at will. Much more efficient. Documents are small, graphics are big. See?
The code has been added to the always excellent jQuery library, and Dean has posted the full script, but there were a few things which I'd like to see, so I refactored it.
In the end it was really just the ability to drop the script on a page, have it isolated in a namespace/Object Literal style, and fire off any script you want to rather than the original init() script.
This still needs testing in every major browser, but IE and Firefox (I don't have Safari except at work) seem to be fine. Opera 8.x uses window.onload and the latest Opera 9 Beta supports the same interface Mozilla supports for DOMContentLoaded.
I'd ask that folks test this and let me know. There might be some mistakes in there somewhere.
Thanks of course go to Dean and John and Matthias, I did very little here but refactor it.
Update: The script has been updated.
Update 2: Fixed some issues with the way the script was implemented for IE, noted in some comments, either here or on one of the other posts about this script.
Possibly Related Articles
Commenting is closed for this article.