异步加载和延迟加载

异步加载:

1:在script元素中加上async属性(为了兼容老版本的IE 加上defer)

2:动态的添加script标签

eg:(function(){

if(window.attachEvent){

window.attachEvent("load", asyncLoad);

}else{

window.addEventListener("load", asyncLoad);

}

var asyncLoad = function(){

var ga = document.createElement('script');

ga.type = 'text/javascript';

ga.async = true;

ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';

var s = document.getElementsByTagName('script')[0];

s.parentNode.insertBefore(ga, s);

}

)();

3:通过eval()执行js代码

延迟加载:

1:settimeOut()

2:将js放在底部

你可能感兴趣的:(异步加载和延迟加载)