H5API 应用缓存(ApplicationCache)

2017-06-14  本文已影响0人  blank的小粉er

浏览器本身的缓存机制

应用缓存的优点(Application Cache)

使用过程

应用程序的原理

manifest文件

 CACHE MANIFEST
 #version: 0.0.1
 CACHE:
 缓存文件.html
 缓存文件.css

 NETWORK:
    不缓存的文件.html

   FALLBACK:
 404.html

applicationCache对象

index.html

<!DOCTYPE html>
<html lang="en" manifest="website.appcache">
<head>
    <meta charset="UTF-8">
    <title>我的网站</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <h1>你的网站很好</h1>
    <button onclick="update()">更新</button>
    <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laboriosam consequatur illum omnis corrupti minima reprehenderit, illo reiciendis velit officiis voluptates officia aliquam optio obcaecati nam hic provident quae eveniet. Ab.
    </p>
    <hr>
    <a href="01.html">01.html</a> <br>
    <hr>
    <div id="box"></div>

    <div id="status"></div>

    <script src="script.js"></script>

    <script>
        var status_box = document.querySelector("#status");

        applicationCache.addEventListener("updateready", function(){

            status_box.innerHTML = "缓存已经更新,请稍后...";

            setTimeout(function(){
                location.reload();
            }, 1000)
        })


        function update(){
            applicationCache.update();
        }
    </script>
</body>
</html>
website.appcache

CACHE MANIFEST
#version 0.1.0
CACHE:
index.html
script.js
style.css

FALLBACK:
offline.html
上一篇 下一篇

猜你喜欢

热点阅读