Http://www.thefutureoftheweb.com/blog/adddomloadevent
Adddomloadevent = (function (){
// Create event function Stack
VaR load_events = [],
Load_timer,
Script,
Done,
Exec,
Old_onload,
Init = function (){
Done = true;
// Kill the timer
Clearinterval (load_timer );
// Execute each function in the stack in the order they were added
While (Exec = load_events.shift ())
Exec ();
If (SCRIPT) script. onreadystatechange = '';
};
Return function (func ){
// If the init function was already ran, just run this function now and stop
If (done) return func ();
If (! Load_events [0]) {
// For Mozilla/opera9
If (document. addeventlistener)
Document. addeventlistener ("domcontentloaded", init, false );
// For Internet Explorer
/* @ Cc_on @*/
/* @ If (@ _ Win32)
Document. Write ("<script id =__ ie_onload defer src = // 0> <\/scr" + "IPT> ");
Script = Document. getelementbyid ("_ ie_onload ");
Script. onreadystatechange = function (){
If (this. readystate = "complete ")
Init (); // call the onload Handler
};
/* @ End @*/
// For Safari
If (/WebKit/I. Test (navigator. useragent) {// sniff
Load_timer = setinterval (function (){
If (/loaded | complete/. Test (document. readystate ))
Init (); // call the onload Handler
}, 10 );
}
// For other browsers set the window. onload, but also execute the old window. onload
Old_onload = Window. onload;
Window. onload = function (){
Init ();
If (old_onload) old_onload ();
};
}
Load_events.push (func );
}
})();