This is an article I saw in the blog park last time. After testing, it is indeed faster than jquery's $(document).ready(function(){....}), and it works well in mainstream browsers such as IE and Firefox. No problem,
I forgot who created it. If the owner sees it, you can contact me and I will add the source of the original text immediately. Please forgive me.
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, go to the second step, store it and kill it together
if (dom.isFunction(fn)) {
if (dom.isReady) {
fn();
//If the DOM has been built, kill one by one
} else {
dom.push(fn);
//Storage loading event
}
}
}
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 event
}
dom.initReady = function() {
if (document.addEventListener) {
document.addEventListener("DOMContentLoaded",
function() {
document.removeEventListener("DOMContentLoaded", arguments.callee, false);
//Clear the loading function
dom.fireReady();
},
false);
} else {
if (document.getElementById) {
document.write("");
document.getElementById("ie-domReady").onreadystatechange = function() {
if (this.readyState === "complete ") {
dom.fireReady();
this.onreadystatechange = null;
this.parentNode.removeChild(this)
}
};
}
}
}
}