异步加载:
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放在底部
网友评论