Wednesday, September 26th, 2007

IEContentLoaded: Yet another DOMContentLoaded

Category: JavaScript, Library

Hedger Wang has a different solution for the DOMContentLoaded issue that Dean Edwards and YUI also solves.

The solution created an element and tries to scroll in it. If an error is thrown, the DOM isn’t loaded yet, so we wait and try again later.


  1. (function (){
  2.     //check IE's proprietary DOM members
  3.     if (!document.uniqueID && document.expando) return;
  5.     //you can create any tagName, even customTag like <document :ready />
  6.     var tempNode = document.createElement('document:ready');  
  7.     try {
  8.         //see if it throws errors until after ondocumentready
  9.         tempNode.doScroll('left');
  11.         //call your function which catch window.onDocumentReady
  12.         alert('window.onDocumentReady()');
  14.         //relaese some memory, if possible
  15.         tempNode = null;
  16.     } catch (err) {
  17.         setTimeout(arguments.callee, 0);
  18.     }
  19. })();

Posted by Dion Almaer at 6:32 am

3.5 rating from 24 votes


Comments feed TrackBack URI

uhm, at least provide a link back to the original author site?

Comment by alvin — September 26, 2007


Comment by Jordan — September 26, 2007

Interesting proprietary stuff but sorry – polling every zero ms isn’t very elegant.

Comment by Matt — September 26, 2007

Haven’t had a chance to test it myself, but I’d like to see some proof of it actually firing before page load. There have been various attempts at fixing this, but not all worked even though they were supposed to.

Comment by Mark Wubben — September 26, 2007

I’ve just commented this function (wrong in my opinion) and I’ve created a cross-browser alternative … of course, based on this doScroll unknown (for me) behaviour :-)

Well done for idea, not so well done its implemnetation but thank You for this post!

Comment by Andrea Giammarchi — September 26, 2007

Very bad if Hedger says it is his own solution.

This have been for a while my own solution, I have blogged about that on Dean Edwards site and I have had tests cases posted here:

Hedger, I am fine if you copy code, just give credits to the original authors…
Hope it works for everybody in most situations. I have also a better solution based on the same trick but will not publish it until I am sure it works OK. I am baking it….

Diego Perini

Comment by Diego Perini — September 27, 2007

Hedger, sorry for the above comments…
I should thank you instead of blaming you :-)
I just realized you are pointing to my site in your pages and credits are present there.

It should be said that in the original code I used the “documentElement” as a source for this trick since that node is the only node available during startup and we can be sure it always exists even in bad/wrongly written pages.

Beside that, scrolling “documentElement” to the left seemed safe to me because the HTML node is normally not used to scroll the page “left”…


Comment by Diego Perini — September 27, 2007

Leave a comment

You must be logged in to post a comment.