为什么localStorage.getItem仅在IE9中失败?

时间:2013-02-22 08:53:13

标签: javascript html internet-explorer-9

这在Chrome和Firefox中非常有效:

var webStorageSupported = ('localStorage' in window) && window['localStorage'] !== null;
if (webStorageSupported && localStorage.getItem("get_list_center") !== null) {
  document.getElementById('mail_listing').innerHTML = localStorage.getItem("get_list_center");
}

但是当我在IE9中查看页面时,我收到了一个未知异常。如果我输入document.getElementById('mail_listing').innerHTML;
,我可以同时获取本地存储中的元素和内容 和localStorage.getItem("get_list_center");分别在控制台中。

error screen

当我尝试将innerHTML指定为本地存储时,会发生未知异常。什么给了???

编辑:根据评论的建议,我试图将本地存储放在一个单独的变量中,然后在innerhtml中分配该变量。事实证明,localstorage确实正在检索HTML格式的代码,但它无法保存整个字符串:

<table width="100%">......
   <TD width=\"7%\" align=right>12K &nbsp;&nbsp;&nbsp;&nbsp; </TD></TR></TBODY></TABLE></DIV>\r\n<DIV style=\"DISPLAY: no   
   [[CUT OFF FROM HERE]]

我认为本地存储可以节省高达5 MB的文本,这适用于Chrome和Firefox,但IE9可能不是这样(编辑:谷歌搜索后,IE9 supports up to 10MB所以很明显它是别的东西)。我该怎么做才能解决这个问题?

Edit2:我已经验证localStorage正确分配整个HTML内容,如果在设置内容后立即检索它:

localStorage.setItem('get_list_center', document.body.innerHTML);
var x = localStorage.getItem('get_list_center'); //GETS THE WHOLE HTML CONTENT CORRECTLY.

只是当我从其他我需要的地方获取内容时,内容会被切断。 localstorage不会被其他任何地方篡改。

1 个答案:

答案 0 :(得分:1)

如果您正在浏览file:网址,这只是我们应该切换到Firefox的众多原因之一 - IE9 hates localStorage on local documents

相关问题