您现在的位置是:网站首页> 编程资料编程资料

HTML5中Localstorage的使用教程HTML5 LocalStorage 本地存储详细概括(多图)html5 localStorage本地存储_动力节点Java学院整理localstorage和sessionstorage使用记录(推荐)详解HTML5 LocalStorage 本地存储 html5本地存储 localStorage操作使用详解HTML5 本地存储 LocalStorage详解解析HTML5中的新功能本地存储localStoragelocalStorage的过期时间设置的方法详解

2021-08-31 1210人已围观

简介 这篇文章主要介绍了HTML5中Localstorage的使用教程,Localstorage被用于浏览器和系统交互的本地传出,需要的朋友可以参考下

什么是localstorage

  前几天在老项目中发现有对cookie的操作觉得很奇怪,咨询下来是要缓存一些信息,以避免在URL上面传递参数,但没有考虑过cookie会带来什么问题:

  ① cookie大小限制在4k左右,不适合存业务数据
  ② cookie每次随HTTP事务一起发送,浪费带宽

  我们是做移动项目的,所以这里真实适合使用的技术是localstorage,localstorage可以说是对cookie的优化,使用它可以方便在客户端存储数据,并且不会随着HTTP传输,但也不是没有问题:

  ① localstorage大小限制在500万字符左右,各个浏览器不一致
  ② localstorage在隐私模式下不可读取
  ③ localstorage本质是在读写文件,数据多的话会比较卡(firefox会一次性将数据导入内存,想想就觉得吓人啊)
  ④ localstorage不能被爬虫爬取,不要用它完全取代URL传参

  瑕不掩瑜,以上问题皆可避免,所以我们的关注点应该放在如何使用localstorage上,并且是如何正确使用。
localstorage的使用
  基础知识

  localstorage存储对象分为两种:

  ① sessionStrage: session即会话的意思,在这里的session是指用户浏览某个网站时,从进入网站到关闭网站这个时间段,session对象的有效期就只有这么长。

  ② localStorage: 将数据保存在客户端硬件设备上,不管它是什么,意思就是下次打开计算机时候数据还在。

  两者区别就是一个作为临时保存,一个长期保存。

  这里来一段简单的代码说明其基本使用:

XML/HTML Code复制内容到剪贴板
  1. <div id="msg" style="margin: 10px 0; border: 1px solid black; padding: 10px; width: 300px;   
  2.   height: 100px;">  
  3. div>  
  4. <input type="text" id="text" />  
  5. <select id="type">  
  6.   <option value="session">sessionStorageoption>  
  7.   <option value="local">localStorageoption>  
  8. select>  
  9. <button onclick="save();">  
  10.   保存数据button>  
  11. <button onclick="load();">  
  12.   读取数据button>  
  13. <script type="text/javascript">  
  14.   var msg = document.getElementById('msg'),   
  15.             text = document.getElementById('text'),   
  16.             type = document.getElementById('type');   
  17.   
  18.   function save() {   
  19.     var str = text.value;   
  20.     var t = type.value;   
  21.     if (t == 'session') {   
  22.       sessionStorage.setItem('msg', str);   
  23.     } else {   
  24.       localStorage.setItem('msg', str);   
  25.     }   
  26.   }   
  27.   
  28.   function load() {   
  29.     var t = type.value;   
  30.     if (t == 'session') {   
  31.       msg.innerHTML = sessionStorage.getItem('msg');   
  32.     } else {   
  33.       msg.innerHTML = localStorage.getItem('msg');   
  34.     }   
  35.   }   
  36. script>  

 真实场景

  实际工作中对localstorage的使用一般有以下需求:

  ① 缓存一般信息,如搜索页的出发城市,达到城市,非实时定位信息

  ② 缓存城市列表数据,这个数据往往比较大

  ③ 每条缓存信息需要可追踪,比如服务器通知城市数据更新,这个时候在最近一次访问的时候要自动设置过期

  ④ 每条信息具有过期日期状态,在过期外时间需要由服务器拉取数据

XML/HTML Code复制内容到剪贴板
  1. define([], function () {   
  2.   
  3.   var Storage = _.inherit({   
  4.     //默认属性   
  5.     propertys: function () {   
  6.   
  7.       //代理对象,默认为localstorage   
  8.       this.sProxy = window.localStorage;   
  9.   
  10.       //60 * 60 * 24 * 30 * 1000 ms ==30天   
  11.       this.defaultLifeTime = 2592000000;   
  12.   
  13.       //本地缓存用以存放所有localstorage键值与过期日期的映射   
  14.       this.keyCache = 'SYSTEM_KEY_TIMEOUT_MAP';   
  15.   
  16.       //当缓存容量已满,每次删除的缓存数   
  17.       this.removeNum = 5;   
  18.   
  19.     },   
  20.   
  21.     assert: function () {   
  22.       if (this.sProxy === null) {   
  23.         throw 'not override sProxy property';   
  24.       }   
  25.     },   
  26.   
  27.     initialize: function (opts) {   
  28.       this.propertys();   
  29.       this.assert();   
  30.     },   
  31.   
  32.     /*   
  33.     新增localstorage   
  34.     数据格式包括唯一键值,json字符串,过期日期,存入日期   
  35.     sign 为格式化后的请求参数,用于同一请求不同参数时候返回新数据,比如列表为北京的城市,后切换为上海,会判断tag不同而更新缓存数据,tag相当于签名   
  36.     每一键值只会缓存一条信息   
  37.     */   
  38.     set: function (key, value, timeout, sign) {   
  39.       var _d = new Date();   
  40.       //存入日期   
  41.       var indate = _d.getTime();   
  42.   
  43.       //最终保存的数据   
  44.       var entity = null;   
  45.   
  46.       if (!timeout) {   
  47.         _d.setTime(_d.getTime() + this.defaultLifeTime);   
  48.         timeout = _d.getTime();   
  49.       }   
  50.   
  51.       //   
  52.       this.setKeyCache(key, timeout);   
  53.       entity = this.buildStorageObj(value, indate, timeout, sign);   
  54.   
  55.       try {   
  56.         this.sProxy.setItem(key, JSON.stringify(entity));   
  57.         return true;   
  58.       } catch (e) {   
  59.         //localstorage写满时,全清掉   
  60.         if (e.name == 'QuotaExceededError') {   
  61.           //            this.sProxy.clear();   
  62.           //localstorage写满时,选择离过期时间最近的数据删除,这样也会有些影响,但是感觉比全清除好些,如果缓存过多,此过程比较耗时,100ms以内   
  63.           if (!this.removeLastCache()) throw '本次数据存储量过大';   
  64.           this.set(key, value, timeout, sign);   
  65.         }   
  66.         console && console.log(e);   
  67.       }   
  68.       return false;   
  69.     },   
  70.   
  71.     //删除过期缓存   
  72.     removeOverdueCache: function () {   
  73.       var tmpObj = null, i, len;  &

相关内容

-六神源码网