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>

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

 

上一篇:vue+bootstrap前端进行分页


下一篇:网页显示访问次数