HTML5本地存储应用sessionStorage和localStorage

html5之前,浏览器要实现数据的存储,一般都是用cookie,但是cookie有域名和大小限定. 

html5流行之后,可以通过localStorage和sessionStorage实现浏览器端的数据存储,这两者有什么特点呢?

sessionStorage
     sessionStorage属于临时会话,数据存储的有效期为:从页面打开到页面关闭的时间段,属于窗口的临时存储,页面关闭,本地存储消失

localStorage

  • 永久存储(可以手动删除数据)

  • 存储量限制 ( 5M )

  • 客户端完成,不会请求服务器处理

  • sessionStorage数据在页面之间不能共享、 而localStorage可以实现页面之间共享

sessionStorage的应用:

<!DOCTYPE html>
<html>
<head>
    <meta charset=UTF-8>
    <title></title>
    <script>
        window.onload = function(){
            var aInput = document.getElementsByTagName('input');
            aInput[0].onclick = function(){
                //sessionStorage: 临时存储, 只在当前页面有效,不能传递到其他页面,页面关闭之后消失
                window.sessionStorage.setItem(name, aInput[3].value );
            };
            aInput[1].onclick = function(){
                alert(window.sessionStorage.getItem(name ));
            };
            aInput[2].onclick = function(){
                window.sessionStorage.removeItem(name );
            };
        }
    </script>
</head>
<body>
<input type=button value=设置 />
<input type=button value=获取 />
<input type=button value=删除 />
<br/>
<input type=text />
</body>
</html>

localStorage的应用

<!DOCTYPE html>
<html>
<head>
    <meta charset=UTF-8>
    <title></title>
    <script>
        window.onload = function(){
            var aInput = document.getElementsByTagName('input');
            aInput[0].onclick = function(){
                //localStorage : 永久性存储
                window.localStorage.setItem(name, aInput[3].value);
                window.localStorage.setItem(name2, 'aaaaa');
            };
            aInput[1].onclick = function(){
                alert( window.localStorage.getItem( name ) );
                alert( window.localStorage.getItem( name2 ) );
            };
            aInput[2].onclick = function(){
                window.localStorage.removeItem(name);
//                window.localStorage.clear();
            };
        }
    </script>
</head>
<body>
<input type=button value=设置 />
<input type=button value=获取 />
<input type=button value=删除 />
<br/>
<input type=text />
</body>
</html>
<!DOCTYPE html>
<html>
<head>
    <meta charset=UTF-8>
    <title></title>
    <script>
        window.onload = function () {
            var aInput = document.getElementsByTagName(input);
            var oT = document.querySelector(textarea);

            if (window.localStorage.getItem(userName)) {
                aInput[0].value = window.localStorage.getItem(userName);
            }

            for (var i = 0; i < aInput.length; i++) {
                if (window.localStorage.getItem('sex') == aInput[i].value) {
                    aInput[i].checked = true;
                }
            }

            if (window.localStorage.getItem(note)) {
                oT.value = window.localStorage.getItem(note);
            }

            window.onunload = function () {
                if (aInput[0].value) {
                    window.localStorage.setItem(userName, aInput[0].value);
                }

                for (var i = 0; i < aInput.length; i++) {
                    if (aInput[i].checked == true) {
                        window.localStorage.setItem('sex', aInput[i].value);
                    }
                }

                if (oT.value) {
                    window.localStorage.setItem('note', oT.value);
                }
            }
        }
    </script>
</head>
<body>
<p>
    用户名: <input type=text/>
</p>

<p>
    性别: <br/>
    <input type=radio name=sex value=男/>男
    <input type=radio name=sex value=女/>女
</p>

<p>
    备注:
    <textarea cols=30 rows=10></textarea>
</p>

</body>
</html>

以上就是HTML5本地存储应用sessionStorage和localStorage的详细内容,更多请关注云资源网其它相关文章!

原文链接:https://www.sudo1.com

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
9.本站默认解压密码为:www.sudo1.com
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。
不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。
如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。
我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!

云资源网 » HTML5本地存储应用sessionStorage和localStorage

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
提示下载完但解压或打开不了?
最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。 若排除这种情况,可在对应资源底部留言,或 联络我们.。
你们有qq群吗怎么加入?
当然有的,如果你是帝国cms、易优cms、和pbootcms系统的爱好者你可以加入我们的QQ千人交流群https://www.sudo1.com/page-qun.html。
  • 会员数(个)
  • 12334资源数(个)
  •        
  • 资源(G)
  •        
  • 今日下载
  • 1405稳定运行(天)

提供最优质的资源集合

立即查看 了解详情