你的位置:首页 > 软件开发 > 网页设计 > 轻松实现localStorage本地存储

轻松实现localStorage本地存储

发布时间:2015-07-31 11:00:09
相信大家都知道HTML5提供了localStorage和sessionStorage两个新功能,基于这两个功能我们可以实现web资源的离线和会话存储,如果你现在还在用Cookie来临时存储网络资源的话,那就太out了,有这么好的东西放着干吗不用呢?下面我将通过一个简单的例子来阐述 ...

相信大家都知道HTML5提供了localStorage和sessionStorage两个新功能,基于这两个功能我们可以实现web资源的离线和会话存储,如果你现在还在用Cookie来临时存储网络资源的话,那就太out了,有这么好的东西放着干吗不用呢?

下面我将通过一个简单的例子来阐述localStorage的用法,实现起来还是相对容易的。例如现在大部分网站都有记录访客第一次访问的信息,如果是第一次访问浏览器在界面上就会弹出一个广告框之类的弹框,之后再访问网站就看不到弹框了,这一效果其实就是用localStorage(之前用Cookie)实现的。

那么现在我们以bootstrap的模态框为例,当用户第一次进入网站的时候弹出模态框,之后就不再弹出,除非用户清除了浏览器的缓存数据。我们的html代码如下:

 1 <div class="modal hide fade" id="demo" data-show="true" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 2   <div class="modal-dialog"> 3       <div class="modal-content"> 4         <div class="modal-header"> 5           <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 6           <h1 class="modal-title text-center step-show1" id="startModalLabel"><span>弹框demo</span></h1>   7         </div> 8         <div class="modal-body"> 9           弹框内容10         </div>11      </div>12    </div>13 </div>                  

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:轻松实现localStorage本地存储

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录