Wednesday, September 26th, 2007
IEContentLoaded: Yet another DOMContentLoaded
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.
-
-
(function (){
-
//check IE's proprietary DOM members
-
if (!document.uniqueID && document.expando) return;
-
-
//you can create any tagName, even customTag like <document :ready />
-
var tempNode = document.createElement('document:ready');
-
try {
-
//see if it throws errors until after ondocumentready
-
tempNode.doScroll('left');
-
-
//call your function which catch window.onDocumentReady
-
alert('window.onDocumentReady()');
-
-
//relaese some memory, if possible
-
tempNode = null;
-
} catch (err) {
-
setTimeout(arguments.callee, 0);
-
}
-
})();
-












uhm, at least provide a link back to the original author site?
http://www.hedgerwow.com/360/dhtml/ie-dom-ondocumentready.html
Clever.
Interesting proprietary stuff but sorry – polling every zero ms isn’t very elegant.
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.
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!
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:
http://javascript.nwbox.com/IEContentLoaded/
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
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”…
Diego
I have updated the IEContentLoaded original script with some minor changes.
The user callback function is now executed from outside the try/catch block.
In this way, errors will not be hidden to developers.
Thanks goes to Jake Archibald for this tip.
@Mark,
this “absolutely” fire before onload, even when no images are present in the page.
@Matt,
I am not polling at 0 millisecs, this have been somebody else attempt…
Please use the original code, a simple interval/timeout is not sufficient in all cases:
IEContentLoaded
Diego