This is an article I saw in the blog Park last time. Article It has been tested that it is indeed faster than jquery's $ (document). Ready (function () {...}), and it is no problem in mainstream browsers such as IE and Firefox,
I forgot who the original is. If the master can contact me, I will add the original source immediately. Sorry.
CopyCode The Code is as follows: var jb51 = new function (){
DOM = [];
Dom. isready = false;
Dom. isfunction = function (OBJ ){
Return object. Prototype. tostring. Call (OBJ) = "[object function]";
}
Dom. Ready = function (FN ){
Dom. initready ();
// If the DOM tree is not built, take the second step and store it together.
If (DOM. isfunction (FN )){
If (DOM. isready ){
FN ();
// If the Dom has been built, kill one
} Else {
Dom. Push (FN );
// Store loading events
}
}
}
Dom. fireready = function (){
If (DOM. isready) return;
Dom. isready = true;
For (VAR I = 0, n = Dom. length; I <n; I ++ ){
VaR fn = Dom [I];
FN ();
}
Dom. Length = 0;
// Clear the event
}
Dom. initready = function (){
If (document. addeventlistener ){
Document. addeventlistener ("domcontentloaded ",
Function (){
Document. removeeventlistener ("domcontentloaded", arguments. callee, false );
// Clear the load function
Dom. fireready ();
},
False );
} Else {
If (document. getelementbyid ){
Document. Write ("<SCRIPT id = \" ie-domready \ "Defer = 'defer' src = \" //: \ "> <\/SCRIPT> ");
Document. getelementbyid ("ie-domready"). onreadystatechange = function (){
If (this. readystate = "complete "){
Dom. fireready ();
This. onreadystatechange = NULL;
This. parentnode. removechild (this)
}
};
}
}
}
}