If you only need to manipulate the DOM, then there is no need to wait for the page to load. We need a quicker approach.
Firefox has domcontentloaded events can be easily resolved, unfortunately, ie did not.
One of MSDN's methods for JScript is obscure, when the page DOM is not loaded, an exception is generated when the DoScroll method is invoked. Then we use the reverse, if not abnormal, then the page DOM loading is complete!
Copy Code code as follows:
function iecontentloaded (W, fn) {
var d = w.document, done = False,
Only Fire once
init = function () {
if (!done) {
Done = true;
FN ();
}
};
Polling for no errors
(function () {
try {
Throws errors until after Ondocumentready
D.documentelement.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 is Diego Perini released this method in 07,
And it's been widely accepted that many open source frameworks now use this approach, such as the Ready in jquery.
If you need to use the domready of IE, it is him.
Usage:
Iecontentloaded (document.getElementById ("test"), test);
function Test () {}