HTML5 离线缓存DEMO示例

来源:互联网 发布:c 多核高级编程 编辑:程序博客网 时间:2024/06/16 03:53

1.index.manifest文件配置

[html] view plain copy
  1. CACHE MANIFEST  
  2. #chrome浏览器支持,火狐浏览器支持度不够好  
  3. #version 1  
  4. #author by guoquanyou  
  5.   
  6. #CACHE:其后列出的是需要缓存的内容  
  7. CACHE:  
  8. index.html  
  9. css/index.css  
  10. img/girl.jpg  
  11.   
  12. #NETWORK:其后列出的是不进行缓存的内容,每次都需要从服务器端获取  
  13. NETWORK:  
  14. *  
  15.   
  16. FALLBACK:  
  17.   
  18. img/pirate1.png img/pirate_back.png  

2.index.html

[html] view plain copy
  1. <!DOCTYPE html>  
  2. <html manifest="index.manifest">  
  3. <head>  
  4. <meta charset="utf-8" />  
  5. <title></title>  
  6. <link rel="stylesheet" href="css/index.css" />  
  7. <script type="text/javascript">  
  8.     //判断浏览器是否能够连接互联网,online表示能够联网,offline表示不能联网;根据浏览器状态自动监听并进行响应【通过断开或开启本地连接进行测试】  
  9.     window.addEventListener("online", function() {  
  10.         alert("您已变成在线状态")  
  11.     }, true);  
  12.     window.addEventListener("offline", function() {  
  13.         alert("您已变成离线状态")  
  14.     }, true);  
  15.     //手动判断浏览器的联网状态  
  16.     /*  if(navigator.onLine){  
  17.         alert("在线");  
  18.     }else{  
  19.         alert("离线");  
  20.     } */  
  21.     //alert("navigator.onLine:"+navigator.onLine);  
  22.     //当index.manifest内容更新时,浏览器检查到存在更新,并进行提示  
  23.     function init() {  
  24.         setInterval("toUpdateCache()", 3000);  
  25.     }  
  26.     function toUpdateCache() {  
  27.         //强制检查服务器上的manifest文件是否有更新  
  28.         applicationCache.update();  
  29.     }  
  30.     applicationCache.onupdateready = function() {  
  31.         if (confirm("本地缓存已被更新,需要刷新页面来获取应用程序最新版本")) {  
  32.             //手动更新本地缓存,只能在onupdateready事件触发时调用  
  33.             applicationCache.swapCache();  
  34.             location.reload();  
  35.         }  
  36.     }  
  37.     //与上述写法完全等效  
  38.     /* applicationCache.addEventListener("updateready",function(){  
  39.         if(confirm("本地缓存已被更新,需要刷新页面来获取应用程序最新版本")){  
  40.             //手动更新本地缓存,只能在onupdateready事件触发时调用  
  41.             applicationCache.swapCache();  
  42.             location.reload();  
  43.         }  
  44.     },true); */  
  45.   
  46.     /* function test(){  
  47.     var msg=document.getElementById("msg");  
  48.     applicationCache.onchecking=function(){  
  49.         msg.innerHTML+="onchecking<br/>";  
  50.     }  
  51.     applicationCache.ondownloading=function(){  
  52.         msg.innerHTML+="ondownloading<br/>";  
  53.     }  
  54.     applicationCache.onprogress=function(){  
  55.         msg.innerHTML+="onprogress<br/>";  
  56.     }  
  57.     applicationCache.onupdateready=function(){  
  58.         msg.innerHTML+="onupdateready<br/>";  
  59.     }  
  60.     applicationCache.oncached=function(){  
  61.         msg.innerHTML+="oncached<br/>";  
  62.     }  
  63.     applicationCache.onerror=function(){  
  64.         msg.innerHTML+="onerror<br/>";  
  65.     }  
  66.       
  67.     } */  
  68. </script>  
  69. </head>  
  70. <body onload="init()">  
  71.     <img src="img/girl.jpg" /><img src="img/pirate1.png" />  
  72.     <img src="img/pirate.png" /> 一个来自服务器端的传说。  
  73.     <div id="msg"></div>  
  74. </body>  
  75. </html>  

3.项目结构


4.使用Tomcat服务器,确认C:\apache-tomcat-8.0.28\conf目录下的web.xml中已经包含以下配置:

[html] view plain copy
  1. <mime-mapping>  
  2.     <extension>appcache</extension>  
  3.     <mime-type>text/cache-manifest</mime-type>  
  4. </mime-mapping>  
5.运行服务器,并访问index.html页面:

1)断开本地连接,然后启用,查看页面的反应情况;

2)修改index.manifest文件,查看页面的反应情况。



原创粉丝点击