利用 localStorage 儲存css js

連接  版本號, 能夠後臺輸出到jsp頁面上javascript

 

    

移動端webapp值得一試:
  • 兼容性好
  • 網速慢,LS讀取+eval大多數狀況下快於304
  • webapp不須要seo,css也能夠緩存,再經過js加載
  • 瀏覽器緩存常常會被清理,LS被清理的概率低一些
/** 
* 插件功能:使用localStorage緩存js和css文件,減小http請求和頁面渲染時間,適用於Web移動端H5頁面製做。 
* 插件做者:zhangqs008@163.com 
* 使用方法:   
*   1.使用此插件前,須要給插件的pageVersion變量賦值,建議變量值由服務器後端輸出,當須要更新客戶端資源時,修改版本值便可。 
* 2.加載Js:因爲js加載有順序要求,因此須要將後加載的腳本做爲前一個腳本的回調參數傳入,如:
* whir.res.loadJs("jquery", "<%= BasePath %>Turntable/Script/jquery-1.8.3.min.js", * function () { * whir.res.loadJs("turntable", "Script/whir.turntable.js", null); * });
* 3.加載css,如:whir.res.loadCss("abc", "/Style/abc.css", null);
*/
var whir = window.whir || {};
whir.res
= { pageVersion: "", //頁面版本,由頁面輸出,用於刷新localStorage緩存

//動態加載js文件並緩存

loadJs: function (name, url, callback) {
if (window.localStorage) {
var xhr;
       
      // 存儲 獲取 
var js = localStorage.getItem(name);
      
        // 若是版本有更新, 或者js沒讀出來, 就用ajax獲取最新的
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;
                 //建立一個link標籤 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.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) {
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 {
       //不支持ls, 只能從新獲取 whir.res.linkCss(url); } },
//往頁面寫入js腳本 writeJs: function (text) { var head = document.getElementsByTagName('HEAD').item(0); var link = document.createElement("script"); link.type = "text/javascript"; link.innerHTML = text; head.appendChild(link); }, //往頁面寫入css樣式 writeCss: function (text) { var head = document.getElementsByTagName('HEAD').item(0); var link = document.createElement("style"); link.type = "text/css"; link.innerHTML = text; head.appendChild(link); }, //往頁面引入js腳本 linkJs: function (url) { var head = document.getElementsByTagName('HEAD').item(0); var link = document.createElement("script"); link.type = "text/javascript"; link.src = url; head.appendChild(link); }, //往頁面引入css樣式 linkCss: function (url) { 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); } }
<script type="text/javascript">  
    //入口函數  
    whir.res.pageVersion = "1002";  //頁面版本,用於檢測是否須要更新緩存  
    whir.res.loadJs("jquery", "/gfdzp201508257998/Turntable/Script/jquery-1.8.3.min.js",  
     function () {  
         whir.res.loadJs("turntable", "/gfdzp201508257998/Turntable/Script/whir.turntable.js", null);  
     });  
    whir.res.loadCss("css", "/gfdzp201508257998/Turntable/Style/css_whir.css", null);  
</script>  
相關文章
相關標籤/搜索