Activate your free membership today | Log-in

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.

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

Posted by Dion Almaer at 6:32 am
8 Comments

+++--
3.5 rating from 22 votes

8 Comments »

Comments feed TrackBack URI

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

http://www.hedgerwow.com/360/dhtml/ie-dom-ondocumentready.html

Comment by alvin — September 26, 2007

Clever.

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:

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

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”…

Diego

Comment by Diego Perini — September 27, 2007

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

Comment by Diego Perini — October 13, 2007

Leave a comment

You must be logged in to post a comment.