localStorge它storage事件

简介:

随着h5患病率和mobile发展。localStorage它不再是一个陌生的词汇。我相信大多数童鞋进行了联系,并用它。但storage事件相信有很多童鞋不清晰甚至没有接触。今天我们主要谈storage。


看w3c大约storage都描写叙述:

4.4 The storage event


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即当前窗体是不会触发这个事件的(当然ie这个特例除外,它包括自己本事也会触发storage事件)。

这句话说的有点绕口。看代码把。


DEMO:

页面a下,有个input框用来存储store,它自身绑定了storage事件。这个时候写入新的数据点击保存,这时新的sotre数据保存了,可是页面a的storage事件没触发,

而页面b下,绑定的storage事件则触发了。(ps:前提页面a和b在同域下,并都是打开状态不同窗体);

页面a代码:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
</head>
<body>
<input type="text" placeholder="input date to save">
<button>save</button>
<script>
  (function(D){
    var val = D.getElementsByTagName("input")[0],
      btn = D.getElementsByTagName("button")[0];
    btn.onclick = function(){
      var value = val.value;
      if(!value) return;
      localStorage.setItem("key", val.value);
    };
    window.addEventListener("storage", function(e){
      console.log(e);
    });
  })(document);
</script>
</body>
</html>

页面b代码:

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

</script>
</body>
</html>

看到这里是不是非常疑惑那storage事件究竟有什么用,多窗体间多通信用到它就是最好选择了,比方某块公用内容区域基础数据部分都是从store中提取的,这个区域中大多数页面中都有,当用户打开多个页面时。在当中一个页面做了数据改动。那其它页面同步更新是不是非常方便(当前页面就要用其它方式处理了),当然用于窗体间通信它作用不只如此,很多其它的大家能够利用它特性去发挥。

ps:

顺带补充下,在上面的demo页面b中storage的events对象的属性经常使用的例如以下: 

 oldValue:更新前的值。假设该键为新添加,则这个属性为null。 

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

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

 

 key:存储store的key名。


另外store虽好可不要贪“杯”噢。大量冗最后但并非不是数据存储灾难更。和store安全性有限,还要注意的是关键数据。

版权声明:本文博客原创文章。博客,未经同意,不得转载。







本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4731799.html,如需转载请自行联系原作者


相关文章
|
6月前
|
监控
jedate change事件监控,使用jedate无法使用change事件
jedate change事件监控,使用jedate无法使用change事件
|
10月前
|
存储 Web App开发 移动开发
📕Local Storage、Session Storage和Cache Storage之间的区别
你知道什么是Cache Storage、Local Storage和Session Storage吗?它们都是一些可以在你的浏览器里保存信息的介质,但是它们有什么不同呢?🤔
381 0
📕Local Storage、Session Storage和Cache Storage之间的区别
Revit空闲事件(Idling Event)增强和外部事件(External Event)
Revit空闲事件(Idling Event)增强和外部事件(External Event)
Revit空闲事件(Idling Event)增强和外部事件(External Event)
uiu
|
存储 对象存储 开发者
本地存储(Local Storage) 和 会话存储(Session Storage)
本地存储(Local Storage) 和 会话存储(Session Storage)
uiu
209 0
本地存储(Local Storage) 和 会话存储(Session Storage)
|
存储 Web App开发 移动开发
storage
在HTML5出现之前,如果开发者需要在客户端存储少量的数据,只能通过cookie来实现,但是cookie存在几个不足点: 每个域名下cookie的大小限制在4KB。 cookie会包含在每个http请求中,这样会导致发送重复的数据。 cookie在网络传输过程中没有加密,存在安全隐患。 在HTML5新增了Web storage功能,Web Storage官方建议为每个网站是5MB,能存储比cookie更多的数据,并且具有比cookie更强大的功能。Web Storage现在已经得到了Firefox、Opera、Chrome、Safari各主流浏览器的支持。
278 0
storage
SAP WM高阶之Storage Type上架策略L下的Mixed Storage(2)
SAP WM高阶之Storage Type上架策略L下的Mixed Storage(2)
SAP WM高阶之Storage Type上架策略L下的Mixed Storage(2)
SAP WM高阶之Storage Type上架策略L下的Mixed Storage(1)
SAP WM高阶之Storage Type上架策略L下的Mixed Storage(1)
SAP WM高阶之Storage Type上架策略L下的Mixed Storage(1)
SAP WM高阶Storage Location Control
SAP WM高阶Storage Location Control
SAP WM高阶Storage Location Control
SAP WM初阶Interim Storage Type不好启用Storage Unit Management
SAP WM初阶Interim Storage Type不好启用Storage Unit Management
SAP WM初阶Interim Storage Type不好启用Storage Unit Management
SAP WM Storage Type Search配置里的Storage Class & WPC标记
SAP WM Storage Type Search配置里的Storage Class & WPC标记
SAP WM Storage Type Search配置里的Storage Class & WPC标记