IE localStorage事件未触发 [英] IE localStorage event misfired

查看:205
本文介绍了IE localStorage事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Internet Explorer 9及更高版本中10,localStorage实施会意外触发事件(此处的大线程:对Chrome的localStorage实施不利吗?)

Within Internet Explorer 9 & 10, the localStorage implementation fires events unexpectedly (great thread here: Bug with Chrome's localStorage implementation?)

有人知道阻止storage事件触发在Internet Explorer中启动更改的选项卡的方法吗?

Does anybody know of a way to stop the storage event from firing on tabs that initiated the change within internet explorer?

例如,单击添加按钮时,以下内容不应显示警报,但在IE中会显示警报:

For example the following shouldn't show an alert when the add button is clicked, but it does in IE:

小提琴: http://jsfiddle.net/MKFLs/

<!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>

附带一提,我在这里用MS打开了一个bug. https://connect.microsoft.com/IE/反馈/详细信息/798684/ie-localstorage-event-misfired

On a side note, I've opened a bug with MS here. https://connect.microsoft.com/IE/feedback/details/798684/ie-localstorage-event-misfired

也许它不会关闭.....

Maybe it won't get closed.....

推荐答案

将脚本更改为以下内容可防止在焦点窗口中处理任何存储事件.

Changing your script to the following prevents the handling of any storage events in the focused window.

这并不是您所要求的,因为我认为这需要对浏览器进行补丁,但是它会使IE 9/10符合规范,而对其他浏览器(除了全局浏览器和听众).

This isn't precisely what you asked, as I believe that would require a patch to the browser, but it causes IE 9/10 to conform to the spec while having no adverse effects on other browsers (other than the global and listeners).

<script type="text/javascript" >
      var focused;

      window.addEventListener('focus', function(){focused=1;}, false);
      window.addEventListener('blur', function(){focused=0;}, false);

      var handle_storage = function (e) {
        if(!focused)
          alert("Storage",focused);
      };

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

</script>

有关更新的,符合标准的行为,请参见此小提琴.

See this fiddle for the updated, conforming behavior.

以下内容也可以工作,并且避免了以监听窗口焦点为代价的侦听器:

The following also works and avoids the listeners at the cost of a runtime check of window focus:

<script type="text/javascript" >

      var handle_storage = function (e) {
        if(!document.hasFocus())
          alert("Storage");
      };

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

</script>

这篇关于IE localStorage事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆