艺海无涯,学无止境。欢迎一起探讨技术和人生,了解我

js延迟加载方案

<script>
function downloadJSAtOnload(){
   var elem = document.createElement("script");
   elem.src="defer.js";
   document.body.appendChild(elem);
};
if(window.addEventListener){
   window.addEventListener("load",downloadJSAtOnload,false);
}else if(window.attachEvent){
   window.attachEvent("onload",downloadJSAtOnload);
}else{
   window.onload = downloadJSAtOnload;
};
</script>