事实证明,jquery 的屌无需多言
查看网络上对ready 的解析也仅仅停留在使用层面上,我就想看看这是怎么实现的,上代码:
// 当页面 完成之后,去除监听函数
function completed() {
document.removeEventListener("DOMContentLoaded", completed);
window.removeEventListener("load", completed);
jQuery.ready();
}
// Catch cases where $(document).ready() is called
// after the browser event has already occurred.
// Support: IE <=9 - 10 only
// Older IE sometimes signals "interactive" too soon
if (document.readyState === "complete" ||
(document.readyState !== "loading" && !document.documentElement.doScroll)) {
// Handle it asynchronously to allow scripts the opportunity to delay ready
// 开始执行 ready 函数
window.setTimeout(jQuery.ready);
} else {
// Use the handy event callback
// 事实上,其是使用了 DOMContentLoaded 这个事件来监听dom 的
document.addEventListener("DOMContentLoaded", completed);
// A fallback to window.onload, that will always work
// 兼容性处理, DOMContentLoaded 只兼容到 IE9 ,8及以下嘛....
window.addEventListener("load", completed);
}