將jquery和公共樣式緩存到localStorage,可以減少Http請求,從而優(yōu)化頁面加載時間,下面的代碼可以實(shí)現(xiàn)此功能:
- <script type="text/javascript">
- //入口函數(shù)
- if (window.localStorage) {
- initJs();
- initCss("css", "/gfdzp201508257998/Turntable/Style/css_whir.css");
- } else {
- addFile("/gfdzp201508257998/Turntable/Script/jquery-1.8.3.min.js", "js");
- addFile("/gfdzp201508257998/Turntable/Script/whir.turntable.js", "js");
- addFile("/gfdzp201508257998/Turntable/Style/css_whir.css", "css");
- }
-
-
- //第一步:加載頁面js:先加載jQuery后加載用戶腳本
- function initJs() {
- var name = "jquery";
- var url = "/gfdzp201508257998/Turntable/Script/jquery-1.8.3.min.js";
- var xhr;
- var js = window.localStorage ? localStorage.getItem(name) : "";
- if (js == null || js.length == 0) {
- if (window.ActiveXObject) {
- xhr = new ActiveXObject("Microsoft.XMLHTTP");
- } else if (window.XMLHttpRequest) {
- xhr = new XMLHttpRequest();
- }
- xhr.open("GET", url);
- xhr.send(null);
- xhr.onreadystatechange = function () {
- if (xhr.readyState == 4 && xhr.status == 200) {
- js = xhr.responseText;
- localStorage.setItem(name, js);
- js = js == null ? "" : js;
- addTxt(js, "js");
- initTurntable(); //確保先引用Jquery
- }
- };
- } else {
- addTxt(js, "js");
- initTurntable();
- }
- }
-
- //加載自定義腳本
- function initTurntable() {
- var name = "turntable";
- var url = "/gfdzp201508257998/Turntable/Script/whir.turntable.js";
- var xhr;
- var js = window.localStorage ? localStorage.getItem(name) : "";
- if (js == null || js.length == 0) {
- if (window.ActiveXObject) {
- xhr = new ActiveXObject("Microsoft.XMLHTTP");
- } else if (window.XMLHttpRequest) {
- xhr = new XMLHttpRequest();
- }
- xhr.open("GET", url);
- xhr.send(null);
- xhr.onreadystatechange = function () {
- if (xhr.readyState == 4 && xhr.status == 200) {
- js = xhr.responseText;
- localStorage.setItem(name, js);
- js = js == null ? "" : js;
- addTxt(js, "js");
- }
- };
- } else {
- addTxt(js, "js");
- }
- }
-
- //第二步:初始化Css
- function initCss(name, url) {
- var xhr;
- var css = window.localStorage ? localStorage.getItem(name) : "";
- if (css == null || css.length == 0) {
- if (window.ActiveXObject) {
- xhr = new ActiveXObject("Microsoft.XMLHTTP");
- } else if (window.XMLHttpRequest) {
- xhr = new XMLHttpRequest();
- }
- xhr.open("GET", url);
- xhr.send(null);
- xhr.onreadystatechange = function () {
- if (xhr.readyState == 4 && xhr.status == 200) {
- css = xhr.responseText;
- localStorage.setItem(name, css);
- css = css == null ? "" : css;
- css = css.replace(/images\//g, "style/images/");
- addTxt(css, "css");
- }
- };
- } else {
- css = css.replace(/images\//g, "style/images/");
- addTxt(css, "css");
- }
- }
-
- //輔助方法1:動態(tài)添加js,css文件引用
- function addFile(url, fileType) {
- var head = document.getElementsByTagName('HEAD').item(0);
- var link;
- if (fileType == "js") {
- link = document.createElement("script");
- link.type = "text/javascript";
- link.src = url;
- } else {
- link = document.createElement("link");
- link.type = "text/css";
- link.rel = "stylesheet";
- link.rev = "stylesheet";
- link.media = "screen";
- link.href = url;
- }
- head.appendChild(link);
- }
-
- //輔助方法2:動態(tài)添加js,css文件內(nèi)容
- function addTxt(text, fileType) {
- var head = document.getElementsByTagName('HEAD').item(0);
- var link;
- if (fileType == "js") {
- link = document.createElement("script");
- link.type = "text/javascript";
- link.innerHTML = text;
- } else {
- link = document.createElement("style");
- link.type = "text/css";
- link.innerHTML = text;
- }
- head.appendChild(link);
- }
- </script>
查看寫入記錄:
封裝成JS插件:
- /**
- * 插件功能:使用localStorage緩存js和css文件,減少http請求和頁面渲染時間,適用于Web移動端H5頁面制作。
- * 插件作者:zhangqs008@163.com
- * 使用方法:
- * 1.使用此插件前,需要給插件的pageVersion變量賦值,建議變量值由服務(wù)器后端輸出,當(dāng)需要更新客戶端資源時,修改版本值即可。
- * 2.加載Js:由于js加載有順序要求,所以需要將后加載的腳本作為前一個腳本的回調(diào)參數(shù)傳入,如:
- * 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("css", "/Style/css_whir.css", null);
- */
- var whir = window.whir || {};
- whir.res = {
- pageVersion: "", //頁面版本,由頁面輸出,用于刷新localStorage緩存
- //動態(tài)加載js文件并緩存
- loadJs: function (name, url, callback) {
- 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(); //回調(diào),執(zhí)行下一個引用
- }
- }
- };
- }
- } else {
- whir.res.writeJs(js);
- if (callback != null) {
- callback(); //回調(diào),執(zhí)行下一個引用
- }
- }
- } 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里的圖片路徑需單獨(dú)處理
- whir.res.writeCss(css);
- }
- };
- }
- } else {
- css = css.replace(/images\//g, "style/images/"); //css里的圖片路徑需單獨(dú)處理
- whir.res.writeCss(css);
- }
- } else {
- 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);
- }
- }
調(diào)用該插件:
- <script type="text/javascript">
- //入口函數(shù)
- 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>
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點(diǎn)擊舉報。