Moh*_*och 11 javascript local-storage internet-explorer-11
我有一个使用localStorage事件的应用程序.一个窗口写入存储,另一个窗口响应.这已经好几个月了,并且仍然在Chrome,FF和IE10上运行,但是由于我的用户开始使用IE11 - 它偶尔会中断.
经过深入调查后,我发现只有当新值中的字符数低于一定数量(我的测量结果为4282)时,IE11 才会触发onstorage事件.
此外,如果已经有相同的密钥下的值,IE只会触发事件如果两个旧值和新的大小一起是在该限制.
一个重要的提示:在所有情况下的价值也被写入到存储.在任何阶段都不会超过存储大小.
这是一个用于演示此问题的小代码示例:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function handle_storage(event)
{
alert("event fired!");
}
window.addEventListener("storage", handle_storage);
var keyName = "keyName";
function writeToStorage()
{
var num = parseInt(document.getElementById("bla").value);
var customValue = "";
for(var i = 0; i<num; i++)
customValue+="c";
localStorage.setItem(keyName, customValue);
}
</script>
</head>
<body >
<button onclick="writeToStorage()">Write</button>
<input type="text" id="bla"/>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
首先尝试使用一个小数字 - 在输入中写入100并单击按钮.您将看到警告说该事件已被触发.
然后尝试使用大量的数字 - 5000.您将看不到警报.
然后尝试数字的组合,你会看到每次新旧值的总和超过限制 - 不会触发任何事件.
这是IE11中的错误吗?
我错过了什么吗?
有没有解决方法?
好.
我们最终做的是一种解决方法.丑陋,但工作.
每次插入存储后,我们还会插入一个"插入通知",这是另一个项目,带有预定义的键.通知的值是实际修改的项的关键.
在'storage'事件的事件处理程序中,我们从事件的newValue属性中检索密钥,然后使用该密钥从存储中检索数据本身.
需要注意的一件非常重要的事情是,如果IE11认为它们足够小,这个流程也可能会获得实际数据的事件.因此,我们必须确保我们不处理通过事件直接检索的任何数据,而只是处理插入通知所获得的数据.
这是一个简单的代码示例,展示了如何实现它,支持IE11和其他浏览器.
var _areInsertNotificationsEnabled; //set this to true if you are running in IE11
var _insertNotifcationsKey = "a_unique_key_for_insert_notifications";
function writeData (key, data)
{
localStorage.setItem(key, storageData);
//If you are running in IE11, after adding the value itself, add an insert notification.
//The notification's value should be the key of the actually modified value.
if(_areInsertNotificationsEnabled)
localStorage.setItem(_insertNotifcationsKey, key);
}
function onStorageChanged(event)
{
//When waiting for insert notifications do not process the data itself,
//so you won't mistakenly process the same data twice, in case the value
//was small enough for IE11 to fire the event
if(!_areInsertNotificationsEnabled && event.key != _insertNotifcationsKey)
processData(event.newValue);
else handleInsertDataNotification(event);
}
function handleInsertDataNotification(event)
{
//pull the actually modified key from the event
var dataKey = event.newValue;
//get the actually modified value
var data = storage.getItem(dataKey);
processData(data);
}
function processData(data)
{
//Do something smart
}
Run Code Online (Sandbox Code Playgroud)
希望这有助于任何人.
| 归档时间: |
|
| 查看次数: |
2870 次 |
| 最近记录: |