jquery如何实现domReady和onload判断的

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

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。