说到本地存储,这玩意真是历尽千辛万苦才走到html5这一步,之前的历史大概如下图所示:
最早的cookies自然是大家都知道,问题主要就是太小,大概也就4kb的样子,而且ie6只支持每个域名20个cookies,太少了。优势就是大家都支持,而且支持得还蛮好。很早以前那些禁用cookies的用户也都慢慢的不存在了,就好像以前禁用javascript的用户不存在了一样。
urdata是ie的东西,垃圾。现在用的最多的是flash吧,空间是cookie的25倍,基本够用。再之后google推出了gears,虽然没有限制,但不爽的地方就是要装额外的插件(没具体研究过)。到了html5把这些都统一了,官方建议是每个网站5mb,非常大了,就存些字符串,足够了。比较诡异的是居然所有支持的浏览器目前都采用的5mb,尽管有一些浏览器可以让用户设置,但对于网页制作者来说,目前的形势就5mb来考虑是比较妥当的。
支持的情况如上图,ie在8.0的时候就支持了,非常出人意料。不过需要注意的是,ie、firefox测试的时候需要把文件上传到服务器上(或者localhost),直接点开本地的html文件,是不行的。
首先自然是检测浏览器是否支持本地存储。在html5中,本地存储是一个window的属性,包括localstorage和ssionstorage,从名字应该可以很清楚的辨认二者的区别,前者是一直存在本地的,后者只是伴随着ssion,窗口一旦关闭就没了。二者用法完全相同,这里以localstorage为例。
if(window.localstorage){ alert('this browr supports localstorage');}el{ alert('this browr does not support localstorage');}
存储数据的方法就是直接给 window.localstorage 添加一个属性,例如: window.localstorage.a 或者 window.localstorage[“a”] 。它的读取、写、删除操作方法很简单,是以键值对的方式存在的,如下:
localstorage.a = 3;//设置a为"3"localstorage["a"] = "sfsf";//设置a为"sfsf",覆盖上面的值localstorage.titem("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() 和 titem() ,清除键值对使用 removeitem() 。如果希望一次性清除所有的键值对,可以使用 clear() 。另外, html5 还提供了一个 key() 方法,可以在不知道有哪些键值的时候使用,如下:
var storage = window.localstorage;function showstorage(){ for(var i=0;i<storage.length;i++){ //key(i)获得相应的键,再用getitem()方法获得对应的值 document.write(storage.key(i)+ " : " + storage.getitem(storage.key(i)) + "<br>"); }}
写一个最简单的,利用本地存储的计数器:
var storage = window.localstorage;if (!storage.getitem("pageloadcount")) storage.titem("pageloadcount",0);storage.pageloadcount = parint(storage.getitem("pageloadcount")) + 1;//必须格式转换document.getelementbyidx_x("count").innerhtml =肖字组词 storage.pageloadcount;showstorage();
不断刷新就能看到数字在一点点上涨,如下图所示:
需要注意的是,html5本地存储只能存字符串,任何格式存储的时候都会被自动转为字符串,所以读取的时候,需要自己进行类型的转换。这也就是上一段代码中parint必须要使用的原因。
另外,在iphone/ipad上有时设置titem()时会出现诡异的quota_exceeded_err错误,这时一般在titem之前,先removeitem(光线折射)就ok了。
html5的本地存储,还提供了一个storage事件,可以对键值对的改变进行监听,使用方法如下:
if(window.addeventlistener){ window.addeventlistener("storage",handle_storage,fal);}el 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 cha我喜欢的明星普通话nge
这里添加两个键值对a和b,并增加一个按钮。给a设置固定的值,当点击按钮时,修改b的值:
<body><p>you have viewed this page <span id="count">0</span> time(s).</p><p><input type="button" value="changestorage" onclick="changes()"/></p><script>var storage = window.localstorage;if (!storage.getitem("pageload长太息count")) storage.titem("pageloadcount",0);storage.pageloadcount = parint(storage.getitem("pageloadcount")) + 1;//必须格式转换document.getelementbyidx_x("count").innerhtml = storage.pageloadcount;showstorage();if(window.addeventlistener){ window.addeventlistener("storage",handle_storage,fal);}el 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] + "<br/>"); }el{ document.write(i + " : object" + "<br/>"); } }}storage.titem("a",5);function changes(){ //修改一个键值,测试storage事件 if(!storage.getitem("b")){storage.titem("b",0);} storage.titem('b',parint(storage.getitem('b'))+1);}function showstorage(){ //循环显示localstorage里的键值对 for(var i=0;i<storage.length;i++){ //key(i)获得相应的键,再用getitem()方法获得对应的值 document.write(storage.key(i)+ " : " + storage.getitem(storage.key(i)) + "<br>"); }}</script></body>
测试发现,目前浏览器对这个支持不太好,仅ipad和firefox支持,而且firefox支持得乱糟糟,e对象根本没有那些属性。ipad支持非常好,用的是e.uri(不是e.url),台式机上的safari不行,诡异。
目前浏览器都带有很好的开发者调试功能,下面分别是chrome和firefox的调试工具查看localstorage:
另外,目前javascript使用非常多的json格式,如果希望存储在本地,可以直接调用json.stringify()将其转为字符串。读取出来后调用json.par()将字符串转为json格式,如下所示:
var details = {author:"isaac","description":"fresheggs","rating":100};storage.titem("details",json.stringify(details));details = json.par(storage.getitem("details"));
json对象在支持localstorage的浏览器上基本都支持,需要注意的是ie8,它支持json,但如果添加了如下的兼容模式代码,切到ie7模式就不行了(此时依然支持localstorage,虽然显示window.localstorage是[object],而不是之前的[object storage],但测试发现getitem()、titem()等均能使用)。
<meta content="ie=7" http-equiv="x-ua-compatible"/>
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助~如果有疑问大家可以留言交流,谢谢大家对www.887551.com的支持!
本文发布于:2023-04-06 11:53:16,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/b30c02905cb9394d7d83356e282f47a2.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:HTML5 LocalStorage 本地存储详细概括(多图).doc
本文 PDF 下载地址:HTML5 LocalStorage 本地存储详细概括(多图).pdf
留言与评论(共有 0 条评论) |