加载小事件

window.onload当页面中的所有静态资源加载完成之后执行代码。
link,src,href都会被加载
 
ready事件当DOM加载完成之后就执行代码
高版本浏览器才兼容
DOMContentLoaded

onreadystatechange

在IE下,当DOM加载完之后,在document.documentElement.doScoll()
的方法,如果DOM没有加载完,调用这个方法会报错。
为了避免报错,所以包一个 try{}catch(error){}

error就为报错的细节,但是try、catch性能不高
 setTimeout(function(){
        document.documentElement.doScroll = function(){
            console.log('有这个方法了');
        }
    },3000);

   function fn(){
        try{
            document.documentElement.doScroll();
            console.log('DOM加载完成');
        }catch(e){
            console.log('进catch了');
            setTimeout(fn,30);
        }
   }

   fn();

  

猜你喜欢

转载自www.cnblogs.com/Allisson/p/10060451.html
今日推荐