星空网 > 软件开发 > Java

cookie VS sessionstorge VS localstorge

虽然cookie , localstorge , sessionstorge三者都有存储的功能,但是还是有区别,

git上地址:https://github.com/lily1010/cookie-storge

我个人的总结如下:

一 Cookie问题

①Cookie是什么

Cookie 是一小段文本信息,伴随着用户请求和页面在 Web 服务器和浏览器之间传递。用户每次访问站点时,Web 应用程序都可以读取 Cookie 包含的信息,简单描述Cookie就像是访问服务器时服务器颁发给用户的“身份证”,下次访问的时候带回这身份证,服务器就能识别信息。

②Cookie特点

--如果不为Cookie设置它的生存周期的话,默认是关闭浏览器的时候就销毁Cookie。

--Cookie默认情况下是不允许出现中文字符的,如果我们要添加具有中文内容的Cookie时,我们需要使用java.net.URLEncoder先对中文进行编码,随后在进行Cookie的添加。读取Cookie时,需要使用java.net.URLDecoder对其进行解码。

--不同的浏览器对Cookie的存储都有一些限制,通常是Cookie数量和Cookie总大小的限制。像火狐对Cookie的限制是每个域名只能有50个Cookie值,总大小不能超过4097个字。

--Cookie在HTTP的头部,如果Cookie的量非常大,要做Cookie做压缩,压缩方法是将Cookie的多个K/V看作是普通的文本,做文本压缩。Cookie的规范中规定,Cookie仅能保存ASCII码为34~126的可见字符。

③Cookie缺点

--每次都跟随用户请求发送给服务器,浪费带宽。

--只能存固定长度的字符

--存不了复杂的数据,比如对象

--cookie还需要指定作用域,不可以跨域调用

--cookie需要前端开发者自己封装setCookie,getCookie方法

 

html5中 sessionstorge和localstorge问题

html5中的Web Storage包括了两种存储方式:sessionStorage和localStorage。为了更大容量存储和复杂数据而设计的除此之外,它俩有setItem,getItem,removeItem,clear等方法

①sessionStorage是什么?


 

sessionStorage用于本地存储一个会话(session)中的数据,这些数据只有在同一个会话中的页面才能访问并且当会话结束后数据也随之销毁。因此sessionStorage不是一种持久化的本地存储,仅仅是会话级别的存储。

②localStorage是什么?

localStorage用于持久化的本地存储,除非主动删除数据,否则数据是永远不会过期的。

③它们的数据处理方法

// 保存数据到sessionStoragesessionStorage.setItem('key', 'value');// 从sessionStorage获取数据var data = sessionStorage.getItem('key');

// 清空sessionStorage选定数据var data = sessionStorage.removeItem("key");

// 清空sessionStorage所有数据var data = sessionStorage.clear();

cookie VS sessionstorge  VS localstorge

cookie VS sessionstorge  VS localstorge

 cookie VS sessionstorge  VS localstorge

cookie VS sessionstorge  VS localstorge

 

三 storage事件

先来看看storage事件的文档说明

The storage event is fired when a storage area changes, as described in the previous two sections (for session storage, for local storage).When this happens, the user agent must queue a task to fire an event with the name storage, which does not bubble and is not cancelable, and which uses the StorageEvent interface, at each Window object whose Document 
object has a Storage object that is affected.

翻译过来,当存储的storage数据发生变化时都会触发它,但是它不同于click类的事件会冒泡和能取消,同时最后这句才是重点,storage改变的时候,触发这个事件会调用所有同域下其他窗口的storage事件,不过它本身触发storage即当前窗口是不会触发这个事件的

下面来看我的文件目录:

cookie VS sessionstorge  VS localstorge

其中test1.html内容是:

<!DOCTYPE html><html>  <head>    <meta charset="utf-8" />    <title></title>    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />  </head>  <body>    <input type="text" placeholder="请输入">     <button>保存数据</button>    <script type="text/javascript">    // 保存数据到sessionStorage    //sessionStorage.setItem('key', 'value');        // 从sessionStorage获取数据    //var data = sessionStorage.getItem('key');          // 清空sessionStorage选定数据    //var data = sessionStorage.removeItem("key");          // 清空sessionStorage所有数据    //var data = sessionStorage.clear();      var val = document.getElementsByTagName("input")[0];     var btn = document.getElementsByTagName("button")[0];     btn.onclick = function(){      var value = val.value;      if(!value) return;      localStorage.setItem("key", val.value);     };      if(window.addEventListener){        window.addEventListener("storage",handle_storage,false);       }else if(window.attachEvent){           window.attachEvent("onstorage",handle_storage);       }      function handle_storage(e){           console.log(e);       document.write("oldValue: "+ e.oldValue + " newValue:" + e.newValue)       }    </script>  </body></html>

 

其中test2.html内容是:

<!DOCTYPE html><html>  <head>    <meta charset="utf-8" />    <title></title>  </head>  <body>    <script type="text/javascript">      if(window.addEventListener){        window.addEventListener("storage",handle_storage,false);       }else if(window.attachEvent){           window.attachEvent("onstorage",handle_storage);       }      function handle_storage(e){           console.log(e);       document.write("oldValue: "+ e.oldValue + " newValue:" + e.newValue)       }    </script>  </body></html>

经过测试得出结论:(ps:前提页面test1.html和test2.html在同域下,并都是打开状态不同窗口)

页面test1.html下,有个input框用来存储store,它自身绑定了storage事件,这个时候写入新的数据点击保存,这时新的sotre数据保存了,但是页面test1.html的storage事件没触发,而页面test2.html下,绑定的storage事件则触发了.

 

问题①:既然改变的是同域下的其他页面数据,那么storage事件应用在那里?

答:多窗口间多通信用到它就是最好选择了,比如某块公用内容区域基础数据部分都是从store中提取的,这个区域中大多数页面中都有,当用户打开多个页面时,在其中一个页面做了数据修改,那其他页面同步更新是不是很方便(当前页面就要用其他方式处理了),当然用于窗口间通信它作用不仅仅如此,更多的大家可以利用它特性去发挥。

问题②:storage的events对象的常用属性有哪些?

 oldValue:更新前的值。如果该键为新增加,则这个属性为null。 

 newValue:更新后的值。如果该键被删除,则这个属性为null。

 url:原始触发storage事件的那个网页的网址。 

 key:存储store的key名;





原标题:cookie VS sessionstorge VS localstorge

关键词:ie

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

亚马逊Sponsored Brand广告现可加入视频,卖家如何玩转视频广告?:https://www.ikjzd.com/articles/115994
思考|疫情过后的几个长期变化:https://www.ikjzd.com/articles/115995
Google广告5大智能出价策略,你都了解吗?:https://www.ikjzd.com/articles/115996
重大利好!今日起全国公路免费通行!长三角货车"通行证"发布无须隔离14天!:https://www.ikjzd.com/articles/115997
点赞中国企业!3000家企业“跨界”造口罩!:https://www.ikjzd.com/articles/115998
加航,美联航再次延长对中国航班停飞!:https://www.ikjzd.com/articles/115999
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
北京丰台区水上乐园哪家好玩?:https://www.vstour.cn/a/366177.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流