比Jquery的document.ready更快的方法_jquery

來源:互聯網
上載者:User
這個是上次在部落格園看到的一篇文章,經測試,確實比jquery的$(document).ready(function(){....})更快,並且在ie和Firefox等主流瀏覽器上都沒問題,

忘了原創是誰了,如果主人看到可以聯絡我, 我會立刻加上原文出處的,敬請見諒。

複製代碼 代碼如下:

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();
//如果沒有建成DOM樹,則走第二步,儲存起來一起殺
if (dom.isFunction(fn)) {
if (dom.isReady) {
fn();
//如果已經建成DOM,則來一個殺一個
} else {
dom.push(fn);
//儲存載入事件
}
}
}
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;
//清空事件
}
dom.initReady = function() {
if (document.addEventListener) {
document.addEventListener("DOMContentLoaded",
function() {
document.removeEventListener("DOMContentLoaded", arguments.callee, false);
//清除載入函數
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)
}
};
}
}
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.