检查HTML 5 localStorage [英] Check for HTML 5 localStorage

查看:129
本文介绍了检查HTML 5 localStorage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查用户浏览器的HTML 5 localStorage兼容性?

How to check for HTML 5 localStorage compability of the users browser?

我一直在使用两张支票:

I have been using two checks:

    if (window.localStorage) {
       // localStorage works!
    }

    if (typeof(localStorage) != 'undefined') {
         // localStorage works!
    }

另外,我在网上看过一个使用try / catch块的函数检查localStorage是否可以添加密钥/数据对,或者抛出错误。

Also, I have seen a function online which uses an try/catch block to check whether localStorage can add a key/data pair, or throws an error.

您会推荐这三种方法中的哪一种,第一种和第二种之间是否存在差异?

Which of those three methods would you recommend, and are there any differences between the first and the second?

推荐答案

@MathiasBynens撰写了一篇很好的文章,比较了不同的方法:

@MathiasBynens has written a nice article about this, comparing different approaches:

http://mathiasbynens.be/notes/localstorage-pattern

这篇关于检查HTML 5 localStorage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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