webstorage是html5中本地存储的解决方案之一,在html5的webstorage概念引入之前除去ie user data、flash cookie、google gears等看名字就不靠谱的解决方案,浏览器兼容的本地存储方案只有使用cookie。有同学可能会问,既然有了cookie本地存储,为什么还要引入webstorage的概念?
cookie肿么了cookie的缺陷是非常明显的
1. 数据大小:作为存储容器,cookie的大小限制在4kb左右这是非常坑爹的,尤其对于现在复杂的业务逻辑需求,4kb的容量除了存储一些配置字段还简单单值信息,对于绝大部分开发者来说真的不知指望什么了。
2. 安全性问题:由于在http请求中的cookie是明文传递的(https不是),带来的安全性问题还是很大的。
3. 网络负担:我们知道cookie会被附加在每个http请求中,在httprequest 和httpresponse的header中都是要被传输的,所以无形中增加了一些不必要的流量损失。
webstoragewebstorage是html新增的本地存储解决方案之一,但并不是为了取代cookie而制定的标准,cookie作为http协议的一部分用来处理客户端和服务器通信是不可或缺的,session正是依赖于实现的客户端状态保持。webstorage的意图在于解决本来不应该cookie做,却不得不用cookie的本地存储。
webstorage提供两种类型的api:localstorage和sessionstorage,两者的区别看名字就有大概了解,localstorage在本地永久性存储数据,除非显式将其删除或清空,sessionstorage存储的数据只在会话期间有效,关闭浏览器则自动删除。两个对象都有共同的api
interface storage { readonly attribute unsigned long length; domstring? key(unsigned long index); getter domstring getitem(domstring key); setter creator void setitem(domstring key, domstring value); deleter void removeitem(domstring key); void clear();};
length:唯一的属性,只读,用来获取storage内的键值对数量。key:根据index获取storage的键名getitem:根据key获取storage内的对应valuesetitem:为storage内添加键值对removeitem:根据键名,删除键值对clear:清空storage对象使用在实现了webstorage的浏览器中,页面有两个全局的对象localstorage和sessionstorage
以localstorage为例,看一段简单的操作代码
var ls=localstorage; console.log(ls.length);//0 ls.setitem('name','byron'); ls.setitem('age','24'); console.log(ls.length);//2 //遍历localstorage for(var i=0;i<ls.length;i++){ /* age : 24 name : byron */ var key=ls.key(i); console.log(key+' : '+ls.getitem(key)); } ls.removeitem('age'); for(var i=0;i<ls.length;i++){ /* name : byron */ var key=ls.key(i); console.log(key+' : '+ls.getitem(key)); } ls.clear();//0 console.log(ls.length);
事件同时html5规定了一个storage事件,在webstorage发生变化的时候触发,可以用此监视不同页面对storage的修改
interface storageevent : event { readonly attribute domstring key; readonly attribute domstring? oldvalue; readonly attribute domstring? newvalue; readonly attribute domstring url; readonly attribute storage? storagearea;};
key:键值对的键oldvalue:修改之前的valuenewvalue:修改之后的valueurl:触发改动的页面urlstoragearea:发生改变的storage在index.php中定义
<a href="test.php" target="_blank">test</a>
window.addeventlistener('storage',function(e){ console.log(e.key+' is changed form '+e.oldvalue+' to '+e.newvalue+' by '+e.url ); console.log(e.storagearea ==localstorage); },false); localstorage.setitem('username','byron');
test.php
localstorage.setitem('username','casper');
在index.php页面点击链接访问test.php时可以看到index.php的控制台输出log:
username is changed form byron to casper by http://localhost/test.php
true
为什么比cookie好1. 从容量上讲webstorage一般浏览器提供5m的存储空间,用来存储视频、图片神马的不够,但对于绝大部分操作足矣
2.安全性上webstorage并不作为http header发送的浏览器,所以相对安全
3.从流量上讲,因为webstorage不传送到服务器,所以不必要的流量可以节省,这样对于高频次访问或者针对手机移动设备的网页还是很不错的。
这并不意味着webstorage可以取代cookie,而是有了webstorage后cookie能只做它应该做的事情了——作为客户端与服务器交互的通道,保持客户端状态。所以仅仅作为本地存储解决方案webstorage是优于cookie的。
注意点1.浏览器兼容性,这个几乎是所有html5新特性中最容易实施的了,因为ie8+的浏览器都支持,在ie7、ie6中可以使用ie user data实现。
2. 由于localstorage和sessionstorage都是对象,所以我饿每年也可以通过”.key”或”[key]”的方式获取、修改键值对,但不推荐这么做
localstorage.username='frank';console.log(localstorage['username']);
3.虽然localstorage存储在本地,但不同的浏览器存储存储数据是独立的,所以在chrome上存储的localstorage在firefox上是获取不到的。
4. localstorage和sessionstorage只能存储字符串类型,对于复杂的对象可以使用ecmascript提供的json对象的stringify和parse来处理,低版本ie可以使用json2.js
5.除了控制台,chrome还为本地存储提供了非常直观的显示方式,调试的时候很方便
更多编程相关知识,请访问:编程学习网站!!
以上就是html5本地存储之webstorage介绍的详细内容。