Adddomloadevent implemented by jquery ready

Source: Internet
Author: User
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 );
}
})();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.