sessionStorage 和 localStorage

使用HTML5 Web存储的localStorage和sessionStorage方式进行Web页面数据本地存储。

页面参考如下图,能将页面上的数据进行本地存储。并能读取存储的数据显示在页面上。

localStorage(本地存储),可以长期存储数据,没有时间限制,一天,一年,两年甚至更长,数据都可以使用。

sessionStorage(会话存储),只有在浏览器被关闭之前使用,创建另一个页面时同意可以使用,关闭浏览器之后数据就会消失。

 

某个博主的测试localStorage兼容情况,如下:
Chrome4+ 开始支持localStorage

Firefox3.5+开始支持localStorage
Firefox1.5+支持globalStorage

IE8+支持localStorage
IE7兼容模式支持localStorage
IE5.5+支持userdata

Safari 4+ 支持localStorage
Opera10.5+支持localStorage

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <style type="text/css">
        textarea {
            width: 300px;
            height: 300px;
        }

        .button {
            width: 100px;
        }
    </style>
</head>
<body onload="init()">
    <script type="text/javascript">
        //使用HTML5 Web存储的localStorage和sessionStorage方式进行Web页面数据本地存储。
        //页面参考如下图,能将页面上的数据进行本地存储。并能读取存储的数据显示在页面上。
        var t1;
        var t2;
        var oStorage;
        var oStorage;
        function init() {//初始化t1,t2
            t1 = document.getElementById("t1");
            t2 = document.getElementById("t2");
            sStorage = window.sessionStorage;
            lStorage = window.localStorage
        }
        function saveSession() {
            sStorage.mydata = t2.value;
            t1.value += "sessionStorage保存mydata:" + t2.value + "\n";
        }
        function readSession() {
            t1.value += "sessionStorage读取mydata:" + sStorage.mydata + "\n";
        }
        function cleanSession() {
            t1.value += "sessionStorage清除mydata:" + sStorage.mydata + "\n";
            sStorage.removeItem("mydata");
        }
        function saveStorage() {
            lStorage.mydata = t2.value;
            t1.value += "localStorage保存mydata:" + t2.value + "\n";
        }
        function readStorage() {
            t1.value += "localStorage读取mydata:" + lStorage.mydata + "\n";
        }
        function cleanStorage() {
            t1.value += "localStorage清除mydata:" + lStorage.mydata + "\n";
            lStorage.removeItem("mydata");
        }
    </script>
    <div>
        <textarea id="t1"></textarea><br />
        <label>需要保存的数据: </label>
        <input type="text" id="t2" /><br />
        <input type="button" class="button" onclick="saveSession()" name="b1" value="session保存" />
        <input type="button" class="button" onclick="readSession()" value="session读取" />
        <input type="button" class="button" onclick="cleanSession()" value="session清除" /><br />
        <input type="button" class="button" onclick="saveStorage()" value="local保存" />
        <input type="button" class="button" onclick="readStorage()" value="local读取" />
        <input type="button" class="button" onclick="cleanStorage()" value="local清除" />
    </div>
</body>
</html>

 

转载于:https://www.cnblogs.com/hzzhero/p/7060505.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值