浏览器的 local storage

 

浏览器  

  1. local storage      本地存储
  2. session storage    会话存储
  3. cookies                  本地存储
  4. 1.     local storage

local Storage 是解决cookies存储空间不足问题.

cookle中每条cookie的存储空间为4K.

local storage 的存储空间一般为5M.这个根据浏览器变化.

local storage 的值类型限定为string类型.如果存储内容多的话会导致页面变卡.

local storage 属于永久性存储.

  1. 2.     local storage 的使用

1° 判断浏览器是否支持

if(!window.localStorage){

            alert("浏览器支持localstorage");

            return false;

        }else{

            //主逻辑业务

        }

2° local storage 的写入

if(!window.localStorage){

            alert("浏览器支持localstorage");

            return false;

        }else{

            var storage=window.localStorage;

            //写入a字段

            storage["a"]=1;

            //写入b字段

            storage.a=1;

            //写入c字段

            storage.setItem("c",3);

            console.log(typeof storage["a"]);

            console.log(typeof storage["b"]);

            console.log(typeof storage["c"]);

        }

 

 

 

 

 

 

 

 

 

3° local storage 的读取

if(!window.localStorage){

            alert("浏览器支持localstorage");

        }else{

            var storage=window.localStorage;

            //写入a字段

            storage["a"]=1;

            //写入b字段

            storage.a=1;

            //写入c字段

            storage.setItem("c",3);

            console.log(typeof storage["a"]);

            console.log(typeof storage["b"]);

            console.log(typeof storage["c"]);

            //第一种方法读取

            var a=storage.a;

            console.log(a);

            //第二种方法读取

            var b=storage["b"];

            console.log(b);

            //第三种方法读取

            var c=storage.getItem("c");

            console.log(c);

        }

4° local storage的删除

1>清除所有

var storage=window.localStorage;

            storage.a=1;

            storage.setItem("c",3);

            console.log(storage);

            storage.clear();

            console.log(storage);

2>删除某个键

var storage=window.localStorage;

            storage.a=1;

            storage.setItem("c",3);

            console.log(storage);

            storage.removeItem("a");

            console.log(storage.a);

 

 

 

 

 

 

5° local storage 的键获取

var storage=window.localStorage;

            storage.a=1;

            storage.setItem("c",3);

            for(var i=0;i<storage.length;i++){

                var key=storage.key(i);

                console.log(key);

            }

 

转载于:https://www.cnblogs.com/enych/p/10499513.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值