var REVERYCACHE = function(){ this.version = 1.42; this.REVERYCACHEBS = 'reveryCaChe-'+rConfig.Partner+'-'; this.include = function(Src,Type,Success){ if(!Src){ return; } if(!(Success instanceof Function)){ Success = function(){} } var srcArray = Src.split(','), newSrc=''; for(var i in srcArray){ var data = this.getData(srcArray[i],0); if(data){ this.initScript(data,Type); }else{ newSrc += ','+encodeURIComponent(srcArray[i]); } } if(newSrc==''){ Success(); return; } var Url = ''; if(Type=='css'){ Url = 'getStyle.php'; }else{ Url = 'getScript.php'; } //回调值 this.getScript(rConfig.RootPath+'res/'+Url+'?src='+newSrc.substr(1),function(data){ for(var dKey in data){ this.initScript(data[dKey],Type); this.saveData(dKey,data[dKey]); } Success(); }.bind(this)); }; this.getScript = function(link,fun){ //回调值 var callback = 'callBack'+(Math.ceil(Math.random()*10000)); var script = document.createElement('script'); script.src = link+'&callBack='+callback; document.body.appendChild(script); script.onload = function(){ var data = window[callback]; if(fun){ fun(data); } document.body.removeChild(script); window[callback] = null; }; script.onerror = function(){ tick('数据加载失败!请刷新页面重试!'); }; }; this.initScript = function(script,type){ if(!script){ return; } if(type=='css'){ var s = document.createElement('style'); s.innerHTML = script; document.head.appendChild(s); }else{ var s = document.createElement('script'); s.innerHTML = script; document.body.appendChild(s); document.body.removeChild(s); } }; this.getData = function(name,isTime){ if(isTime!==0){ isTime = 1; } var time = Date.parse(new Date()); var data = window.localStorage.getItem(this.REVERYCACHEBS+name); if(data){ //判断数据是否过期 if(isTime){ var endTime = parseFloat(data.substr(0,13)); if(endTime>time){ return data.substr(13); }else{ return ''; } }else{ return data; } }else{ return ''; } }; this.saveData = function(name,data,time){ var endTime; if(time){ endTime = Date.parse(new Date())+(time*1000); }else{ endTime=''; } window.localStorage.setItem(this.REVERYCACHEBS+name,endTime+data); }; this.removeCaChe = function(){ for(var i in window.localStorage){ if(i.indexOf(this.REVERYCACHEBS)!=-1){ window.localStorage.removeItem(i); } } }; this.update = function(){ var version = this.REVERYCACHEBS+'Version'; if(parseFloat(window.localStorage.getItem(version))!=this.version){ this.removeCaChe(); window.localStorage.setItem(version,this.version); } }; this.update(); }; var rCache = new REVERYCACHE();