码迷,mamicode.com
首页 > Web开发 > 详细

jquery如何实现domReady和onload判断的

时间:2014-10-16 00:35:11      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   sp   div   on   cti   

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判断的

标签:style   blog   color   io   ar   sp   div   on   cti   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!