Jes*_*sse 12 javascript html5 internet-explorer local-storage
在Internet Explorer 9和10中,localStorage实现意外触发事件(这里有一个很好的主题:Chrome的localStorage实现的Bug?)
有没有人知道如何阻止storage事件触发在Internet Explorer中启动更改的选项卡?
例如,单击添加按钮时,以下内容不应显示警告,但在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>
Run Code Online (Sandbox Code Playgroud)
编辑:在旁注,我在这里打开了一个MS的错误.https://connect.microsoft.com/IE/feedback/details/798684/ie-localstorage-event-misfired
也许它不会被关闭.....
Wya*_*att 14
将脚本更改为以下内容可防止在焦点窗口中处理任何存储事件.
这并不是你所要求的,因为我认为这需要修补程序,但它会使IE 9/10符合规范,同时对其他浏览器(全局和监听器除外)没有任何负面影响.
<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>
Run Code Online (Sandbox Code Playgroud)
请参阅此小提琴,了解更新的符合标准的行为.
编辑:以下也可以工作,并以窗口焦点的运行时检查为代价来避免侦听器:
<script type="text/javascript" >
var handle_storage = function (e) {
if(!document.hasFocus())
alert("Storage");
};
window.addEventListener("storage", handle_storage, false);
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3196 次 |
| 最近记录: |