javascript - Chrome 的 localStorage 实现有问题?

标签 javascript html google-chrome

进一步this question ,在将函数绑定(bind)到 Chrome 8.0.552.224 中存储对象的更改事件时,我得到了一个奇怪的结果。

测试:

<!DOCTYPE html>
<html>
  <head>
    <title>Chrome localStorage Test</title>
    <script type="text/javascript" >

      var handle_storage = function () {
        alert('storage event');
      };

      window.addEventListener("storage", handle_storage, false);

    </script>
  </head>
  <body>
    <button id="add" onclick="localStorage.setItem('a','test')">Add</button>
    <button id="clear" onclick="localStorage.clear()">Clear</button>
  </body>
</html>
  • 在两个 Chrome 窗口中打开页面,一个窗口有两个标签,
  • 点击“添加”按钮

当我执行此操作时,我会在第二个选项卡和第二个窗口上显示一个警告框,但不会在调用事件的选项卡(我单击的)上显示。据我了解,我应该会看到三个警告框(每个打开的选项卡对应一个)。

这是一个错误吗?还有其他人有这种行为吗?如果不是,你运行的是什么版本?还是我完全弄错了?

最佳答案

更新和最终结论

事实证明,规范实际上这是期望的行为,因此 IE9 的实现被破坏了

4.2 The sessionStorage attribute

When the setItem(), removeItem(), and clear() methods are called on a Storage object x ... if the methods did something, then in every HTMLDocument ... [that is] associated with the same storage area, other than x, a storage event must be fired....

因此,正如我们所见,规范在明确说明这是指定行为方面做得非常糟糕。这就是 Opera 10 的实现被破坏的原因,这很可能也是 IE9 的实现被破坏的原因。

我们从中学到了什么?始终阅读规范的每个单个,单词(特别是如果您正在实现这些内容...)。

旧答案

正如您所说,这里的基本行为是“调用除当前页面之外的所有页面”。

有一个旧的 Chrome Bug Report从去年七月开始。

正如人们在那里读到的那样,Firefox 也有同样的“问题”。我用最新的 nightly 测试了这个,仍然像在 Chrome 中一样运行。

Opera 11 中的另一项测试表明,这一定是某种规范行为,因为 Opera 11 做完全相同的事情,但 Opera 10 确实在所有窗口/选项卡上触发事件。遗憾的是,Opera 11 的官方更新日志并未声明对此行为有任何更改。

通读 specification ,那里没有任何说明这种行为。我唯一能找到的是:

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.

Note: This includes Document objects that are not fully active, but events fired on those are ignored by the event loop until the Document becomes fully active again.

那么,Note 是什么意思?

来自另一个specification :

A Document is said to be fully active when it is the active document of its browsing context, and either its browsing context is a top-level browsing context, or the Document through which that browsing context is nested is itself fully active.

没有意义?是的。它对我们有任何帮助吗?没有。

所以我们(在 JavaScript Chatroom 中)在#whatwg 上戳了一下,看看这一切是关于什么的,到目前为止还没有任何回应。我们会在收到任何回复后立即更新我的答案。

暂时结束

Firefox、Chrome、Safari 和 Opera 具有完全相同的行为。也就是说,它们不会在有机会访问 localStorage 的选项卡/窗口上触发。

但 IE9 Beta 的行为类似于 Opera 10,因此它会在所有 选项卡/窗口上触发。

由于 localStorage 规范的作者在谷歌的研发部门工作,我几乎不怀疑 Chrome 会犯这个错误。由于在 Bugzilla 上没有关于此的 Bug,并且 Opera 在 11 中更改了行为,因此这似乎是它应该工作的方式。仍然没有答案为什么它以这种方式工作以及为什么 IE9 的行为不同,但我们仍在等待 #whatwg 的回应。

关于javascript - Chrome 的 localStorage 实现有问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4679023/

相关文章:

javascript - 如何超越Bing Map 25个航点限制?

html - 部分悬停由 CSS3 激活和不激活

php - 生成 <ul> 样式的面包屑丰富网页摘要

javascript - 仅在 JS 中定位多个选择器

javascript - v8 中 node.js 和 chrome 的区别

php - 如何将 Chrome 请求重复为 curl 命令?

javascript - 从数组中选择元素时出现意外标识符

javascript - 如何使用 moment.js 获取年、月和日中的 2 个日期之间的差异

javascript - 使用 jQuery 和 REST 创建 Amazon S3 存储桶

javascript - chrome xml 下载警告