为什么在Internet Explorer中访问localStorage对象会抛出错误? [英] Why does accessing the localStorage object in Internet Explorer throw an error?

查看:141
本文介绍了为什么在Internet Explorer中访问localStorage对象会抛出错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理客户端问题,其中Modernizr意外地检测不到Internet Explorer 9中对 localStorage 对象的支持。我的页面正确使用了HTML 5 doctype( <!DOCTYPE html> )和开发人员工具报告该页面具有IE9的浏览器模式和IE9标准的文档模式,所以我希望这可以工作。

I'm working on a client issue where Modernizr unexpectedly does not detect the support for the localStorage object in Internet Explorer 9. My page correctly uses the HTML 5 doctype (<!DOCTYPE html>) and the developer tools report the page has a Browser Mode of IE9 and a Document Mode of IE9 standards so I would expect this to work.

我调试了以下尝试 / catch 在Modernizr中阻止,并在访问 localStorage 对象时发现JavaScript错误。

I've debugged into the following try/catch block in Modernizr and discovered a JavaScript error is thrown as soon as the localStorage object is accessed.

tests['localstorage'] = function() {
    try {
        localStorage.setItem(mod, mod);
        localStorage.removeItem(mod);
        return true;
    } catch(e) {
        return false;
    }
};

在某些机器上,JavaScript错误的消息是系统找不到文件指定。。在其他情况下,它只是无效的参数。并且Internet Explorer会在它抛出错误之前的5分钟内阻塞。

On some machines the message of the JavaScript error is The system cannot find the file specified.. On others it is just Invalid argument. and Internet Explorer blocks for exactly 5 minutes before it throws the error.

是什么导致访问 localStorage 对象在Internet Explorer上抛出错误?

What is causing accessing the localStorage object to throw an error here on Internet Explorer?

推荐答案

我发现最低级子域是否与其中一个保留的设备名称相匹配,如 Internet Explorer上的文件掩码和文件名属性的限制然后访问 localStorage 对象将引发错误。

I've discovered if the lowest level subdomain matches one of the reserved device names as documented at Restrictions on the File Mask and File Name Properties on Internet Explorer then accessing the localStorage object will throw an error.

这个问题很可能发生,因为内部Internet Explorer在访问 localStorage 对象以满足 存储对象初始化步骤

This problem is likely happening because internally Internet Explorer is attempting to access the file system using a reserved device name when accessing the localStorage object in order to satisfy the Storage object initialization steps.

这肯定是一个边缘情况,但如果你的页面来自服务器的最低级别子域名是完全符合任何 con prn aux 时钟$ nul com1 com2 com3 com4 com5 com6 com7 com8 com9 lpt1 lpt2 lpt3 lpt4 lpt5 lpt6 lpt7 lpt8 ,或 lpt9 (例如 http://prn.example.com )那么这就是你看到这个问题的原因。

It's certainly very much an edge case but if your page is comes from a server whose lowest level subdomain is exactly any of con, prn, aux, clock$, nul, com1, com2, com3, com4, com5, com6, com7, com8, com9, lpt1, lpt2, lpt3, lpt4, lpt5, lpt6, lpt7, lpt8, or lpt9 (e.g. http://prn.example.com) then this could well the reason why you are seeing this problem.

在这种情况下选择一个不是保留设备名称的低级子域解决了这个问题。

Choosing a lower level subdomain that wasn't a reserved device name in this situation solved the problem.

这篇关于为什么在Internet Explorer中访问localStorage对象会抛出错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆