localstorage 更新监测 storage事件
1、存储更新监测
存储状态监测的原理是storage事件。storage事件说明:
https://developer.mozilla.org/zh-CN/docs/Web/API/StorageEvent
storage事件是注册在window上的。
2、示例
同域下2个文件,分别为test.html和test1.html。
test.html文件为:
<!DOCTYPE html>
<html lang="zh"> <head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>storage事件</title>
</head> <body>
<script type="text/javascript">
setTimeout(function(){
window.localStorage.setItem('a', 2)
},1000)
window.addEventListener("storage", function(e) {
console.log(e)
});
</script>
</body> </html>
test1.html文件为:
<!DOCTYPE html>
<html lang="zh"> <head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>storage事件</title>
</head> <body>
<script type="text/javascript">
window.localStorage.setItem('a', 1)
window.addEventListener("storage", function(e) {
console.log(e)
});
</script>
</body> </html>
运行2个文件,test1.html的控制台输出为:

即能监测到localStorage的变化。
3、说明
(1)是同域的不同文件会监测到存储值的变化。
(2)同一个文件,存储值的变化,监测不到。如:
<!DOCTYPE html>
<html lang="zh"> <head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>storage事件</title>
</head> <body>
<script type="text/javascript">
window.localStorage.setItem('a', 1)
setTimeout(function(){
window.localStorage.setItem('a', 2)
},2000)
setTimeout(function(){
window.localStorage.setItem('a', 3)
},3000)
setTimeout(function(){
window.localStorage.setItem('a', 4)
},4000)
window.addEventListener("storage", function(e) {
console.log(e)
});
</script>
</body> </html>
运行上述文件,控制台没有输出内容。
localstorage 更新监测 storage事件的更多相关文章
- localStorage、sessionStorage详解,以及storage事件使用
有关localStorage和sessionStorage的特性. localStorage本身带有方法有 添加键值对:localStorage.setItem(key,value),如果key存在时 ...
- localStorge它storage事件
随着h5患病率和mobile发展.localStorage它不再是一个陌生的词汇.我相信大多数童鞋进行了联系,并用它.但storage事件相信有很多童鞋不清晰甚至没有接触.今天我们主要谈storage ...
- html5 storage事件
HTML5 虽然很多年了,但是真的了解不不够不够.主题说的是 storage时间,说起对 storage 事件的了解还是从 QQ音乐 说起. QQ音乐的主页是 https://y.qq.com , 而 ...
- storage 事件监听
在公司的一次内部分享会上, 偶然知道了这个H5的新事件, 解决了我之前的一个bug. 事情是这样的, 第A网页上显示的数量的总和, 点击去是B页面, 可以进行管理, 增加或者删除, 当用户做了增删操作 ...
- sessionStorage html5客户端本地存储之sessionStorage及storage事件
可以看一下<JavaScript本地存储实践(html5的localStorage和ie的userData)>sessionStorage和上文中提到的localStorage非常相识,方 ...
- js页面间通信方法(storage事件)(浏览器页面间通信方法)
在写页面的时候有时会遇到这样的需求,需要两个页面之间传递数据或者一个事件.这个时候,就需要用到我今天所要讲的storage事件,学习这个事件之前,需要先了解localStorage的用法.具体用法可以 ...
- html5客户端本地存储之sessionStorage及storage事件
首先您可以看一下<JavaScript本地存储实践(html5的localStorage和ie的userData)>sessionStorage和上文中提到的localStorage非常相 ...
- localStore的storage事件
两个浏览器窗口间通信 两个浏览器窗口间通信 补充一下,这里的通讯指遵守同源策略情况下. 为了吸引读者的兴趣,先把demo放到前面:下面有几个我自己写的演示多页面通讯的demo, 为了正常运行,请用 ...
- storage事件 js页面间通信
1.概述 https://developer.mozilla.org/en-US/docs/Web/Events/storage localStorage 或者sessionStorage存储的数据发 ...
随机推荐
- MATLAB的一些使用的快捷键整理
1.用TAB键可以实现缩进,怎么缩进和取消缩进呢? 在使用脚本编写matlab的程序时,我们通过选中需要的程序,按下tab键就能缩进整个程序.同样的,当我们需要取消缩进时,我们的快捷方法就是:shif ...
- 最短路(Floyd)-hdu1317
题目链接:https://vjudge.net/problem/HDU-1317 题目描述: 题意:玩家起始有100个能量点,刚开始在起始房间中,每个房间外有一条单向的路径通往其他房间(一个房间可能通 ...
- javascript监听数组变化
, ]; ); , ]; ); , ]; ); ]; , ]; ); ); ); break; } if (inserted) { ob.observeArray(inserted); } // 通知 ...
- How to check for null/empty/whitespace values with a single test?
SELECT column_name FROM table_name WHERE TRIM(column_name) IS NULL
- 牛客练习赛35-函数的魔法-floyd
函数的魔法 思路 :如果 可以从A到B最终 都会是233范围内的数字进行转换,注意 这里 建图 为单向图 这个运算未必符合交换关系. #include<bits/stdc++.h> us ...
- python中正则表达式 re.findall 用法
在python中,通过内嵌集成re模块,程序媛们可以直接调用来实现正则匹配. 其中,re.findall() 函数可以遍历匹配,可以获取字符串中所有匹配的字符串,返回一个列表. 在python源代码中 ...
- EF Core使用CodeFirst在MySql中创建新数据库以及已有的Mysql数据库如何使用DB First生成域模型
官方教程:https://docs.microsoft.com/en-us/aspnet/core/data/?view=aspnetcore-2.1 使用EF CodeFirst在MySql中创建新 ...
- ELK日志分析方案
针对公司项目微服务化,随着项目及服务器的不断增多,决定采用ELK(Elasticsearch+Logstash+Kibana)日志分析平台进行微服务日志分析. 1.ELK整体方案 1.1 ELK架构图 ...
- c/c++保存日志程序模板
//输出日志 int PrintRunInfo(char *fmt, ...) { FILE* fp; fp = fopen("cgi_log.txt","a+&qu ...
- 一道颇有难度的JavaScript题
上次分享了一道题,大家反响不错,很开心自己写的东西有人愿意花时间去看,也给了自己莫大的鼓舞,其实做题虽然不比真正的编程,但是也能够让你发现一些你之前没有注意到的语言层面的问题.所以,这次再分享一道稍微 ...