码迷,mamicode.com
首页 > 其他好文 > 详细

localStorage与sessionStorage 的区别

时间:2016-10-12 11:15:50      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

通过一枚页面计数器来区别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>

 

localStorage与sessionStorage 的区别

标签:

原文地址:http://www.cnblogs.com/baiyygynui/p/5951613.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!