问题描述
Internet Explorer 11(Windows 7 版本)中的 localStorage
对象包含某些函数的字符串表示,而不是您所期望的本机调用.
The localStorage
object in Internet Explorer 11 (Windows 7 build) contains string representations of certain functions instead of native calls as you would expect.
这只会与普通 JavaScript 中断,像 JSFiddle 这样的网站对此代码没有问题,但我怀疑这是因为有 localStorage
可以纠正它.
This only breaks with vanilla JavaScript and sites like JSFiddle have no problem with this code but I suspect it's because there are localStorage
polyfills in place that correct it.
以这个HTML页面代码为例:
Take this HTML page code for example:
<!DOCTYPE html>
<script>
localStorage.setItem('test', '12345');
alert(localStorage.getItem('test'));
localStorage.clear();
</script>
这在我安装的所有浏览器中都运行良好,除了 IE11.第一行 'SCRIPT5002: Function expected' 出现错误.
This works perfectly well in all my installed browsers except for IE11. An error occurs on the first line 'SCRIPT5002: Function expected'.
看看setItem
函数在IE开发者工具控制台中实际是什么类型,说明它是一个字符串...?
Taking a look at what type the setItem
function actually is in the IE developer tools console, states that it's a string...?
typeof localStorage.setItem === 'string' // true
打印出 setItem
的字符串显示如下:
Printing out the string for setItem
displays the following:
"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;
}"
奇怪的是,并不是所有的函数都被字符串替换了,例如,对应的getItem
函数确实是一个函数,并且按预期工作.
Oddly enough, not all functions have been replaced with strings, for example, the corresponding getItem
function is indeed a function and works as expected.
typeof localStorage.getItem === 'function' // true
将文档模式(模拟)更改为 10 或 9 仍然不能解决问题,并且都会导致相同的错误.将文档模式更改为 8 会出现以下错误对象不支持此属性或方法",这是预期的,因为 IE8 不支持 localStorage代码>.
Changing the document mode (emulation) to 10 or 9 still doesn't resolve the problem and both result in the same error. Changing the document mode to 8 gives the following error 'Object doesn't support this property or method' which is expected since IE8 doesn't support localStorage
.
是否有其他人在 Windows 7 上遇到与 IE11 相同的问题,其中 localStorage
对象似乎损坏/损坏"?
Is anyone else having the same issue with IE11 on Windows 7 where the localStorage
object seems 'broken/corrupt'?
推荐答案
原来这是 Windows 7 SP1 的基本版本 IE11 (11.0.9600.16428) 中的问题.
Turns out this is a problem in the base version of IE11 (11.0.9600.16428) for Windows 7 SP1.
安装补丁后更新到11.0.9600.16476(更新版本 11.0.2 - KB2898785)问题得到解决.可以在 补丁下载页面.
After installing a patch to update to 11.0.9600.16476 (update version 11.0.2 - KB2898785) the issue gets resolved. Links to other versions of Windows (32-bit etc.) can be found at the bottom of the patch download page.
这篇关于JavaScript localStorage 对象在 Windows 7 上的 IE11 中损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!