说到本地存储,这玩意真是历尽千辛万苦才走到html5这一步,之前的历史大概如下图所示:
最早的cookies自然是大家都知道,问题主要就是太小,大概也就4kb的样子,而且ie6只支持每个域名20个cookies,太少了。优势就是大家都支持,而且支持得还蛮好。很早以前那些禁用cookies的用户也都慢慢的不存在了,就好像以前禁用javascript的用户不存在了一样。
userdata是ie的东西,垃圾。现在用的最多的是flash吧,空间是cookie的25倍,基本够用。再之后google推出了gears,虽然没有限制,但不爽的地方就是要装额外的插件(没具体研究过)。到了html5把这些都统一了,官方建议是每个网站5mb,非常大了,就存些字符串,足够了。比较诡异的是居然所有支持的浏览器目前都采用的5mb,尽管有一些浏览器可以让用户设置,但对于网页制作者来说,目前的形势就5mb来考虑是比较妥当的。
支持的情况如上图,ie在8.0的时候就支持了,非常出人意料。不过需要注意的是,ie、firefox测试的时候需要把文件上传到服务器上(或者localhost),直接点开本地的html文件,是不行的。
首先自然是检测浏览器是否支持本地存储。在html5中,本地存储是一个window的属性,包括localstorage和sessionstorage,从名字应该可以很清楚的辨认二者的区别,前者是一直存在本地的,后者只是伴随着session,窗口一旦关闭就没了。二者用法完全相同,这里以localstorage为例。
if(window.localstorage){ alert('this browser supports localstorage'); }else{ alert('this browser does not support localstorage'); }
存储数据的方法就是直接给 window.localstorage 添加一个属性,例如: window.localstorage.a 或者 window.localstorage[a] 。它的读取、写、删除操作方法很简单,是以键值对的方式存在的,如下:
localstorage.a = 3;//设置a为3 localstorage[a] = sfsf;//设置a为sfsf,覆盖上面的值 localstorage.setitem(b,isaac);//设置b为isaac var a1 = localstorage[a];//获取a的值 var a2 = localstorage.a;//获取a的值 var b = localstorage.getitem(b);//获取b的值 localstorage.removeitem(c);//清除c的值
这里最推荐使用的自然是 getitem() 和 setitem() ,清除键值对使用 removeitem() 。如果希望一次性清除所有的键值对,可以使用 clear() 。另外, html5 还提供了一个 key() 方法,可以在不知道有哪些键值的时候使用,如下:
var storage = window.localstorage; function showstorage(){ for(var i=0;i2fbbdc9c3515a3c578e5eb88e1f5a546); } }
写一个最简单的,利用本地存储的计数器:
var storage = window.localstorage; if (!storage.getitem(pageloadcount)) storage.setitem(pageloadcount,0); storage.pageloadcount = parseint(storage.getitem(pageloadcount)) + 1;//必须格式转换 document.getelementbyidx_x(count).innerhtml = storage.pageloadcount; showstorage();
不断刷新就能看到数字在一点点上涨,如下图所示:
需要注意的是,html5本地存储只能存字符串,任何格式存储的时候都会被自动转为字符串,所以读取的时候,需要自己进行类型的转换。这也就是上一段代码中parseint必须要使用的原因。
另外,在iphone/ipad上有时设置setitem()时会出现诡异的quota_exceeded_err错误,这时一般在setitem之前,先removeitem()就ok了。
html5的本地存储,还提供了一个storage事件,可以对键值对的改变进行监听,使用方法如下:
if(window.addeventlistener){ window.addeventlistener(storage,handle_storage,false); }else if(window.attachevent){ window.attachevent(onstorage,handle_storage); } function handle_storage(e){ if(!e){e=window.event;} //showstorage(); }
对于事件变量e,是一个storageevent对象,提供了一些实用的属性,可以很好的观察键值对的变化,如下表:
property
type
description
key
string
the named key that was added, removed, or moddified
oldvalue
any
the previous value(now overwritten), or null if a new item was added
newvalue
any
the new value, or null if an item was added
url/uri
string
the page that called the method that triggered this change
这里添加两个键值对a和b,并增加一个按钮。给a设置固定的值,当点击按钮时,修改b的值:
6c04bd5ca3fcae76e30b72ad730ca86d e388a4556c0f65e1904146cc1a846beeyou have viewed this page d7095a27822ebfc9324399e7e2aaea81054bdf357c58b8a65c66d7c19c8e4d114 time(s).94b3e26ee717c64999d7867364b1b4a3 e388a4556c0f65e1904146cc1a846bee01d8bff6816d3fe04eb5c6a227498a5994b3e26ee717c64999d7867364b1b4a3 3f1c4e4b6b16bbbd69b2ee476dc4f83a var storage = window.localstorage; if (!storage.getitem(pageloadcount)) storage.setitem(pageloadcount,0); storage.pageloadcount = parseint(storage.getitem(pageloadcount)) + 1;//必须格式转换 document.getelementbyidx_x(count).innerhtml = storage.pageloadcount; showstorage(); if(window.addeventlistener){ window.addeventlistener(storage,handle_storage,false); }else if(window.attachevent){ window.attachevent(onstorage,handle_storage); } function handle_storage(e){ if(!e){e=window.event;} showobject(e); } function showobject(obj){ //递归显示object if(!obj){return;} for(var i in obj){ if(typeof(obj[i])!=object || obj[i]==null){ document.write(i + : + obj[i] + 076402276aae5dbec7f672f8f4e5cc81); }else{ document.write(i + : object + 076402276aae5dbec7f672f8f4e5cc81); } } } storage.setitem(a,5); function changes(){ //修改一个键值,测试storage事件 if(!storage.getitem(b)){storage.setitem(b,0);} storage.setitem('b',parseint(storage.getitem('b'))+1); } function showstorage(){ //循环显示localstorage里的键值对 for(var i=0;i2fbbdc9c3515a3c578e5eb88e1f5a546); } } 2cacc6d41bbb37262a98f745aa00fbf0 36cc49f0c466276486e50c850b7e4956
测试发现,目前浏览器对这个支持不太好,仅ipad和firefox支持,而且firefox支持得乱糟糟,e对象根本没有那些属性。ipad支持非常好,用的是e.uri(不是e.url),台式机上的safari不行,诡异。
目前浏览器都带有很好的开发者调试功能,下面分别是chrome和firefox的调试工具查看localstorage:
另外,目前javascript使用非常多的json格式,如果希望存储在本地,可以直接调用json.stringify()将其转为字符串。读取出来后调用json.parse()将字符串转为json格式,如下所示:
var details = {author:isaac,description:fresheggs,rating:100}; storage.setitem(details,json.stringify(details)); details = json.parse(storage.getitem(details));
json对象在支持localstorage的浏览器上基本都支持,需要注意的是ie8,它支持json,但如果添加了如下的兼容模式代码,切到ie7模式就不行了(此时依然支持localstorage,虽然显示window.localstorage是[object],而不是之前的[object storage],但测试发现getitem()、setitem()等均能使用)。
<meta content="ie=7" http-equiv="x-ua-compatible"/>
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
基于html5陀螺仪实现移动动画效果
怎样用h5计算手机摇动次数
以上就是h5的localstorage本地存储使用详解的详细内容。