Web移动端使用localStorage缓存Js和CSS文件
将js css文件缓存到localstorage区可以减少页面在加载时与HTTP请求的交互次数,从而优化页面的加载时间.
电脑端使用localstorage是测试没有问题的,但是用android的apk打包之后,导致localstorage失效,无法进行缓存,查阅资料之后,得知解决方法:
安卓代码:
mWebView.getSettings().setDomStorageEnabled(true); mWebView.getSettings().setAppCacheMaxSize(1024*1024*8); String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath(); mWebView.getSettings().setAppCachePath(appCachePath); mWebView.getSettings().setAllowFileAccess(true); mWebView.getSettings().setAppCacheEnabled(true);
之后便可以使用localstorage进行缓存,
缓存方法已经封装成了通用的插件:
具体代码如下(网上查询所得):
/** * 插件功能:使用localStorage缓存js和css文件,减少http请求和页面渲染的时间,使用于web移动端H5页面的制作 * 使用方法: * 1.使用此插件前,需要给插件的pageVersion变量赋值,建议变量值由服务器后端输出,当需要更新客户资源时,修改变量值即可 * 2.加载js:由于js加载有顺序要求,所以需要将后加载的脚本作为前一个加载的回调函数参数传入,如: * whir.res.loadJs ("jquery","<%= BasePath %>TurnTable/Script/jquery.min.js", * function () { * whir.res.loadJs(name,url,null) * }); */ var whir = window.whir || {}; whir.res = { pageVersion : "",//版本,有页面输入,用语刷新localStorage缓存 loadJs : function (name, url, callback) {//动态加载js文件并缓存 if (window.localStorage) { var xhr; var js = localStorage.getItem(name); if (js == null || js.length == 0 || this.pageVersion != localStorage.getItem("version")) { if (window.ActiveXObject) { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } else if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } if (xhr != null) { xhr.open("GET",url); xhr.send(null); xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { js = xhr.responseText; localStorage.setItem(name,js); localStorage.setItem("version",whir.res.pageVersion); js = js == null ? "" : js; whir.res.writeJs(js); if (callback != null) { callback(); } } } } } else { whir.res.writeJs(js); if (callback != null) { callback();//回调,执行下一个引用 } } } else { whir.res.linkJs(url); } }, loadCss : function (name,url) { if (window.localStorage) { var xhr; var css = localStorage.getItem(name); if (css == null || css.length == 0 || this.pageVersion != localStorage.getItem("version")) { if (window.ActiveXObject) { xhr = new ActiveXObject("Microsoft.XNLHTTP"); } else if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } if (xhr != null) { xhr.open("GET",url); xhr.send(null); xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { css = xhr.responseText; localStorage.setItem(name,css); localStorage.setItem("version",whir.res.pageVersion); css = css == null ? "" : css; css = css.replace(/images\//g,"style/images/");//css里的图片路径需要单独处理 whir.res.writeCss(css); } } } } else { css = css.replace(/images\//g,"style/images/");//css里的图片路径需要单独处理 whir.res.writeCss(css); } } else { whir.res.linkCss(url); } }, writeJs : function (text) {//往页面写入js\脚本 var head = document.getElementsByTagName('HEAD').item(0);//获取head var link = document.createElement("script"); link.type = "text/javascript"; link.innerHTML = text; head.appendChild(link); }, writeCss : function (text) {//往页面写入css样式 var head = document.getElementsByTagName("HEAD").item(0); var link = document.createElement("style"); link.type = "text/css"; link.innerHTML = text; head.appendChild(link); }, linkJs : function (url) {//往页面引入js var head = document.getElementsByTagName('HEAD').item(0);//获取head var link = documenr.createElement("script"); link.type = "text/javascript"; link.src = url; head.appendChild(link); }, linkCss : function (url) {//往页面引入css var head = document.getElementsByTagName('HEAD').item(0); var link = document.createElement("link"); link.type = "text/css"; link.rel = "stylesheet"; link.rev = "stylesheet"; link.media = "screen"; link.href = url; head.appendChild(link); } };
第一次加载时可能会慢,第二次就能明显提升访问速度...