Can#39;t see localstorage event across tabs(无法跨标签查看本地存储事件)
问题描述
我正在尝试创建一个关于使用 localStorage 在发生更改时触发我的应用程序中的选项卡的简单概念验证.根据我看过的其他文章,我知道这是可能的.我了解 规范 声明该事件将永远触发除了我所在的页面 - 这实际上是我想要的.但是,我似乎无法让这个简单的演示真正发挥作用.
I'm trying to create a simple proof-of-concept regarding the use of localStorage to trigger tabs in my application when changes occur. I know this is possible based on other articles I've seen. I understand the spec states the event will fire on ever page except the one I'm on - this is in fact what I want. However, I can't seem to get this simple demo to actually work.
我采用了下面的代码,并打开了它的多个选项卡.使用我的 Chrome 开发工具,我可以在控制台中检查 >localStorage 并查看按下添加"和清除"按钮之前和之后的值 - 实际上,它们在将键值对存储和清除到本地时按需要运行存储,但事件没有触发警报.
I've taken the code below, and opened up multiple tabs of it. Using my Chrome Dev Tools, I can check >localStorage in the console and see the value before and after the "Add" and "Clear" buttons are pressed - they are in fact functioning as desired in storing and clearing the key value pair into local storage, yet the event isn't firing the alert.
我什至在每个选项卡的 Chrome 开发工具中的 javascript 中放置了断点,但我似乎永远无法在打开的任何选项卡中点击onStorageEvent"功能.
I even placed breakpoints in the javascript in my Chrome Dev Tools of each tab, yet I can never seem to get the "onStorageEvent" function to hit in any tab opened.
我做错了什么?
<!DOCTYPE html>
<html>
<head>
<title>Tab1</title>
</head>
<body>
<button id="add" onclick="localStorage.setItem('tab','changed')">Add</button>
<button id="clear" onclick="localStorage.clear()">Clear</button>
<script type="text/javascript">
function onStorageEvent() {
alert("storage event: " + localStorage.getItem("tab"));
}
window.addEventListener('storage', onStorageEvent, false);
</script>
</body>
</html>
推荐答案
为了让 window.addEventListener
在 storage
上工作:
In order for the window.addEventListener
to work on storage
:
您必须使用网络服务器(
http://abcd/
或http://domain
)访问页面
you MUST access the page using web-server (
http://a.b.c.d/
orhttp://domain
)
- 直接访问(使用
file:///
或c:file.html
将不工作.
(chrome和ie忽略它,firefox和safari无论如何都可以运行).
我也会考虑删除第 3 个,它与 DOM
树中的元素无关,并且可能会导致麻烦(让浏览器拥有它的默认值).
I would consider also removing the 3rd, it is not relevant to elements in your DOM
tree, and it might cause troubles (let the browser have it's defaults).
此代码已在 Chrome 52、Firefox 47+48、IE 11、Safari 5.7.1 中测试
This code was tested in Chrome 52, Firefox 47+48, IE 11, Safari 5.7.1
<!DOCTYPE html>
<html>
<head>
<title>Tab1</title>
</head>
<body>
<button id="add" onclick="localStorage.setItem('tab','changed')">Add</button>
<button id="clear" onclick="localStorage.clear()">Clear</button>
<script type="text/javascript">
function onStorageEvent() {
alert("storage event: " + localStorage.getItem("tab"));
}
window.addEventListener('storage', onStorageEvent);
</script>
</body>
</html>
这篇关于无法跨标签查看本地存储事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法跨标签查看本地存储事件
基础教程推荐
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 直接将值设置为滑块 2022-01-01