DOM ready in javascript IE

Source: Internet
Author: User

If you only need to perform operations on the DOM, there is no need to wait until all the pages are loaded. We need a faster method.
Firefox has a DOMContentLoaded event that can be easily solved. Unfortunately, IE does not.
MSDN's method of JSCRIPT is unremarkable. When the page DOM is not loaded, an exception occurs when the doScroll method is called. If no exception occurs, the page DOM is loaded!
Copy codeThe Code is as follows:
Function IEContentLoaded (w, fn ){
Var d = Doc ument, done = false,
// Only fire once
Init = function (){
If (! Done ){
Done = true;
Fn ();
}
};
// Polling for no errors
(Function (){
Try {
// Throws errors until after ondocumentready
D.doc umentElement. doScroll ('left ');
} Catch (e ){
SetTimeout (arguments. callee, 50 );
Return;
}
// No errors, fire
Init ();
})();
// Trying to always fire before onload
D. onreadystatechange = function (){
If (d. readyState = 'complete '){
D. onreadystatechange = null;
Init ();
}
};
}

This function was released by Diego Perini in,
It is also widely recognized that many open-source frameworks use this method, such as ready in JQuery.

If you need to use IE's DomReady later, it will be him.

Usage:
IEContentLoaded (document. getElementById ("test"), test );

Function test (){}

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.