在Windows 7上的IE11中破坏了JavaScript localStorage对象

Bru*_*Dev 44 javascript html5 windows-7 local-storage internet-explorer-11

localStorageInternet Explorer 11(Windows 7版本)中的对象包含某些函数的字符串表示形式,而不是您期望的本机调用.

这只打破了香草JavaScript和像JSFiddle这样的网站对这段代码没有任何问题,但我怀疑是因为有localStorage正确的polyfill 来纠正它.

以此HTML页面代码为例:

<!DOCTYPE html>
<script>
  localStorage.setItem('test', '12345');
  alert(localStorage.getItem('test'));
  localStorage.clear();
</script>
Run Code Online (Sandbox Code Playgroud)

除了IE11之外,这在我安装的所有浏览器中运行良好.第一行' SCRIPT5002:预期功能 ' 发生错误.

看一下setItemIE开发人员工具控制台中函数的实际类型,说明它是一个字符串......?

    typeof localStorage.setItem === 'string' // true
Run Code Online (Sandbox Code Playgroud)

打印出字符串以setItem显示以下内容:

"function() {
var result;
callBeforeHooks(hookSite, this, arguments);
try {
result = func.apply(this, arguments);
} catch (e) {
callExceptHooks(hookSite, this, arguments, e);
throw e;
} finally {
callAfterHooks(hookSite, this, arguments, result);
}
return result;
}"
Run Code Online (Sandbox Code Playgroud)

奇怪的是,并非所有函数都被字符串替换,例如,相应的getItem函数确实是一个函数并且按预期工作.

    typeof localStorage.getItem === 'function' // true
Run Code Online (Sandbox Code Playgroud)

将文档模式(仿真)更改为10或9仍然无法解决问题,并且两者都会导致相同的错误.将文档模式更改为8会出现以下错误" 对象不支持此属性或方法 ",这是IE8不支持的预期localStorage.

是否有其他人在Windows 7上遇到与IE11相同的问题,localStorage对象似乎"破坏/腐败"?

Bru*_*Dev 25

原来这是Windows 7 SP1的基本版IE11(11.0.9600.16428)中的一个问题.

安装补丁更新到11.0.9600.16476(更新版11.0.2 - KB2898785)问题得到解决.可以在修补程序下载页面的底部找到指向其他Windows版本(32位等)的链接.


som*_*sar 5

这不仅仅是 IE11 的错。

可能WEINRE被注入到页面中。它挂钩到几个系统函数中以提供开发人员工具功能,但 IE11错误地解释了对localStoragesessionStorage属性的分配,并将挂钩函数转换为字符串,就好像它们是将要存储的数据一样。

apache/cordova-weinre repo 中一条评论说:

        #In IE we should not override standard storage functions because IE does it incorrectly - all values that set as
        # storage properties (e.g. localStorage.setItem = function()[...]) are cast to String.
        # That leads to "Function expected" exception when any of overridden function is called.
        object[property] = hookedFunction  unless navigator.userAgent.match(/MSIE/i) and (object is localStorage or object is sessionStorage)
Run Code Online (Sandbox Code Playgroud)

看起来要么是旧版本的 WEINRE 正在使用,要么此更改尚未正式发布(自 2013 年以来一直存在)。