首页 > Web开发 > 详细

jquery如何实现domReady和onload判断的

时间:2014-10-15 23:59:42      阅读:539      评论:0      收藏:0      [点我收藏+]
function ready(fn) {
    var completed = function() {

        if ( document.addEventListener ) {
            document.removeEventListener( "DOMContentLoaded", completed, false );
            window.removeEventListener( "load", completed, false );

        } else {
            document.detachEvent( "onreadystatechange", completed );
            window.detachEvent( "onload", completed );
        }

        fn();
    }
    if (document.readyState === ‘completed‘) { // 页面本身已加载结束
        fn();
    } else if (document.addEventListener) {

        document.addEventListener("DOMContentLoaded", completed, false);
        window.addEventListener("load", completed, false);
    } else {
        document.attachEvent("onreadystatechange", completed);
        window.attachEvent("load", completed);
    }
}

ready(function() {
    console.log(1);
});

console.log(3);
window.onload = function() {
    console.log(2);
}
console.log(4);


// 3,4,1,2

 

jquery如何实现domReady和onload判断的

原文:http://www.cnblogs.com/qiangspecial/p/4027544.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!