本地存储

7 篇文章 0 订阅

HTML5中 Web Storage 的出现,主要是为了弥补使用 Cookie 作为本地存储的不足。Cookie 存储的数据量非常小,而且数据会自动携带到请求头里,但服务器端可能并不关心这些数据,所以会造成带宽的浪费。

Web Storage 提供了两个存储对象:localStorage 和 sessionStorage。

知识点:

window.sessionStorage//会话存储

window.localStorage//本地保存数据


sessionStorage与localStorage的区别:

1.localStorage:永久存储

   sessionStorage:当浏览器窗口关闭时失效

2.localStorage:对于同源窗口可以共享

   sessionStorage:不同的窗口不共享

setItem(key, value) 设置存储内容

getItem(key) 读取存储内容

removeItem(key) 删除键值为key的存储内容

clear() 清空所有存储内容


<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
*{
margin: 0;
padding: 0;
outline: none;
}
.box{
width: 300px;
height: 250px;
margin: 100px auto;
text-align: center;
background:pink ;
overflow: hidden;
}
input[type="text"],input[type="password"]{
width: 190px;
height: 30px;
display: block;
margin-left:50px;
padding-left: 10px;
}
input[type="text"]:first-child{
margin-top: 50px;
}
input[type="button"]{
width: 60px;
height: 30px;
border: none;
}
</style>
</head>
<body>
<div class="box">
<input type="text" id="name" placeholder="input your name"/><br />
<input type="password" id="pw" placeholder="input your password"/><br />
<input type="button" value="resiger" />
<input type="button" value="login" />
</div>
<script>
var txt=document.querySelectorAll("input");
var userName=txt[0];
var password=txt[1];
var resiger=txt[2];
var login=txt[3];
var jsonInfo=JSON.parse(window.sessionStorage.getItem("info")||"[]");//读取存储内容
resiger.οnclick=function(){
var n=userName.value;
var pw=password.value;
if(n.trim()==""||pw.trim()==""){
alert("请完善信息!!");
return;
}
var json={
"name":n,
"password":pw
};
for(var i=0;i<jsonInfo.length;i++){
console.log(jsonInfo[i].name);
if(n==jsonInfo[i].name){
alert("用户已存在!!!");
return;
}
}
jsonInfo.push(json);
window.sessionStorage.setItem("info",JSON.stringify(jsonInfo));//设置存储内容
alert("注册成功!!!");
}


login.οnclick=function(){
var n=userName.value;
var pw=password.value;
for(var i=0;i<jsonInfo.length;i++){
console.log(jsonInfo[i].name);
if(n==jsonInfo[i].name&&pw==jsonInfo[i].password){
alert("登录成功!!!");
return;
}
alert("密码与用户名不匹配!!!");
}
}
</script>
</body>

</html>


在 setItem 时,可能会达到大小限制,最好加上错误捕捉

try {  

  localStorage.setItem(key, value);

} catch(e) {

  if (isQuotaExceeded(e)) {

    // Storage full, maybe notify user or do some clean-up

  }

}

 

function isQuotaExceeded(e) {  

  var quotaExceeded = false;

  if (e) {

    if (e.code) {

      switch (e.code) {

        case 22:

          quotaExceeded = true;

          break;

        case 1014:

          // Firefox

          if (e.name === 'NS_ERROR_DOM_QUOTA_REACHED') {

            quotaExceeded = true;

          }

          break;

      }

    } else if (e.number === -2147024882) {

      // Internet Explorer 8

      quotaExceeded = true;

    }

  }

  return quotaExceeded;

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值