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

js 自己创建ready多个可以依次加载

时间:2015-07-30 19:04:58      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

js会把相同的方法名给覆盖了,很多时候我们都无法再页面加载的时候写多个onload事件,这样只有最后一个才能起效,所以从网上找了找,最后决定自己写一个,例子很简单,希望有高人来指导指导

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>javascript_window.ready</title>
</head>
<body>
<script type="text/javascript">

(function(w){
    w.readyLength=0;
    w.readyFunction=[];
    w.ready=function(load){
        w.readyFunction[w.readyLength]=load;
        w.readyLength++;
    }
    w.init=function(){
        clearInterval(w.readyById);
        w.readyFunction.reverse();
        while(w.readyFunction.length-1>=0){
            w.readyFunction[w.readyFunction.length-1]();
            w.readyFunction.pop();
            w.readyLength--;
        }
    }
    if(‘onreadystatechange‘ in w.document){
        w.document.onreadystatechange=onreadystatechange;
    }else{
        w.readyById = setInterval(onreadystatechange,10);
    } 
    function onreadystatechange(){
        console.log(w.document.readyState)
        if(w.document.readyState == "complete"){
            setTimeout(init,1);
        }else if(w.document.addEventListener){
            w.document.addEventListener( "DOMContentLoaded", init, false );
            w.addEventListener(‘load‘,init,false)
        }else{
            document.attachEvent( "onreadystatechange", init );
            w.attachEvent(‘onload‘,init)
        }
    }
}(this));
(function(){
window.ready(function(){
    console.log(‘ready1‘)
});
window.ready(function(){
    console.log(‘ready2‘)
})

window.ready(function(){
    console.log(‘ready3‘)
})
}());;;


</script>


</body>
</html>

 

js 自己创建ready多个可以依次加载

标签:

原文地址:http://www.cnblogs.com/tongchuanxing/p/ready_onload_js_javascript.html

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