RT,一个网页要怎么做才能故意先不加载JS脚本,等需要的时候再加载进去!
function addScript(jsfile,callback){
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.type= 'text/javascript';
script.onload = script.onreadystatechange = function() {
if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete" ) {
script.onload = script.onreadystatechange = null;
if(callback&&typeof(callback)== 'function'){
callback();
}
}
};
script.src= jsfile;
head.appendChild(script);
}
function jsLoaded(){
alert('ok');
}
调用方式,在需要的地方
addScript(js路径,jsLoaded);
最好是使用RequireJS这样的框架,它会自动帮你管理js的加载。
http://www.ruanyifeng.com/blog/2012/11/require_js.html
lazyload
还有好几个的,忘了,$LAB好像是一个
学习了~~~~~~~~~~~~~~
写一个函数用来加载脚本,触发时调用这个函数