localStorage与sessionStorage 的区别

通过一枚页面计数器来区别localStorage与sessionStorage。

通过一个计数变量pageconut,每刷新页面,增加的是localStorage的数量,而sessionStorage只记录当前访问数量;

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="content-type" content="text/html;charset=gb2312" />
        <title>页面访问数</title>
    </head>
    <body>
        <script>
            if(localStorage.pagecount){
                localStorage.pagecount = Number(localStorage.pagecount) + 1;
            }else{
                localStorage.pagecount = 1;
            }
            
            document.write('总访问数:' + localStorage.pagecount);
            
            if(sessionStorage.pagecount){
                sessionStorage.pagecount = Number(sessionStorage.pagecount) + 1;
            }else{
                sessionStorage.pagecount = 1;
            }
            
            document.write('当前访问数:' + sessionStorage.pagecount);
        </script>
    </body>
</html>

 

posted @ 2016-10-12 09:37  礼拜16  阅读(590)  评论(0编辑  收藏  举报