HTML 5 Web 存储 localStorage - 没有时间限制的数据存储 sessionStorage - 针对一个 session 的数据存储

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="result"></div>
    <div id="result2">显示点击次数</div>
    <script>
        if(typeof(Storage)!=="undefined"){
            localStorage.setItem("last","gggg");//存储
            document.getElementById("result").innerHTML=localStorage.getItem("last");   //获取         
        }
        else{
            document.getElementById("result").innerHTML=localStorage.getItem("你的浏览器不支持");   //获取 
        }
        //存储访问次数
        if(localStorage.pagecount){
            localStorage.pagecount=Number(localStorage.pagecount)+1;
        }
        else{
            localStorage.pagecount=1;
        }
        document.write("vistit "+localStorage.pagecount+" times");

        //sessionStorage
        if(sessionStorage.pagecount){
                sessionStorage.pagecount=Number(sessionStorage.pagecount)+1;
            }
            else{
                sessionStorage.pagecount=1;
            }
        document.getElementById("result2").innerHTML=sessionStorage.pagecount;   //获取 
       

    </script>
</body>
</html>

 

posted @ 2020-08-18 16:55  DurianTRY  阅读(372)  评论(0编辑  收藏  举报