javascript onDOMReady 在DOM树载入后执行

时间:2013-05-08 02:10:02  来源:第二电脑网  作者:第二电脑网

  第二电脑网导读:p;   var timer;//定时器句柄        ready=function(fn) {               if (isReady )                      fn.call( document);  ...
  正文:

(function(){
       var isReady=false;//判断onDOMReady方法是否已经被执行过
       var readyList= [];//把需要执行的方法先暂存在这个数组里
       var timer;//定时器句柄
       ready=function(fn) {
              if (isReady )
                     fn.call( document);
              else
                     readyList.push( function() { return fn.call(this);});
              return this;
       }
       var onDOMReady=function(){
              for(var i=0;i<readyList.length;i++){
                     readyList[i].apply(document);
              }
              readyList = null;
       }
       var bindReady = function(evt){
              if(isReady) return;
              isReady=true;
              onDOMReady.call(window);
              if(document.removeEventListener){
                     document.removeEventListener("DOMContentLoaded", bindReady, false);
              }else if(document.attachEvent){
                     document.detachEvent("onreadystatechange", bindReady);
                     if(window == window.top){
                            clearInterval(timer);
                            timer = null;
                     }
              }
       };
       if(document.addEventListener){
              document.addEventListener("DOMContentLoaded", bindReady, false);
       }else if(document.attachEvent){
              document.attachEvent("onreadystatechange", function(){
                     if((/loaded|complete/).test(document.readyState)) 《javascript onDOMReady 在DOM树载入后执行》由第二电脑网原创提供,转载请注明:http://www.002pc.com/master/College/Programming/JavaScript/26143.html


关于《javascript onDOMReady 在DOM树载入后执行》文章的评论

站内搜索: 高级搜索

热门搜索: Windows style 系统 tr IP QQ CPU 安装 function 注册 if td