javascript - 当值太大时,IE11 不会触发本地存储事件

标签 javascript local-storage internet-explorer-11

我有一个使用 localStorage 事件的应用程序。一个窗口写入存储,另一个窗口使用react。这几个月来一直运行良好,在 Chrome、FF 和 IE10 上仍然运行良好,但自从我的用户开始使用 IE11 - 它偶尔会中断。

经过深入调查,我发现 IE11 仅在新值中的字符数低于特定数字(根据我的测量为 4282)时才会触发 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>

首先尝试使用一个小数字 - 在输入中写入 100 并单击按钮。您将看到提示该事件已被触发的提示。
然后尝试使用较大的数字 - 5000。您将看不到任何警报。

然后尝试数字组合,您会看到每次新旧值之和都超过限制 - 不会触发任何事件。

这是 IE11 中的错误吗?
我错过了什么吗?
有什么解决方法吗?

最佳答案

好的。

我们最后所做的是一种解决方法。丑陋,但工作。

每次插入存储后,我们还会插入一个“插入通知”,这只是另一个项目,带有预定义的键。通知的值是实际修改项的键。

在“存储”事件的事件处理程序中,我们从事件的 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
}

希望这对任何人都有帮助。

关于javascript - 当值太大时,IE11 不会触发本地存储事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21139931/

相关文章:

html - flex 属性在 IE 中不起作用

html - 我怎样才能让更广泛的 child 从他们绝对定位的 parent 中溢出?

javascript - 比较两个 JavaScript 字符串日期

php - 如何使用 AJAX 更新选择菜单 w/out <div> inside form

javascript - 仅当使用 NodeJS 更改 Firebase 数据时才执行

javascript - 在 ios 上禁用 flex 滚动(橡皮筋效果)

javascript - 本地存储一个由 .php 脚本递增的 javascript 变量

java - 消除输入 QR - 由 selenium 自动化的 Whatsapp web - Java

javascript - VBA:从 InternetExplorer 对象访问 LocalStorage

javascript - 在 Internet Explorer 中使用 Shadow dom