如何检测浏览器是否支持HTML5本地存储

TK1*_*123 80 html javascript html5

ls existIE7中的以下代码警报:

if(window.localStorage) {
    alert('ls exists');
} else {
    alert('ls does not exist');
}
Run Code Online (Sandbox Code Playgroud)

IE7并不真正支持本地存储,但这仍然会提醒它.也许这是因为我在IE7浏览器中使用IE9并使用IE9开发人员工具进行文档模式.或者这可能是测试LS是否支持的错误方法.什么是正确的方法?

此外,我不想使用Modernizr,因为我只使用了一些HTML5功能,加载一个大脚本不值得它只是为了检测对这些事情的支持.

And*_*eas 93

您不必使用modernizr,但可以使用他们的方法来检测是否localStorage支持

现代化的github
测试localStorage

// In FF4, if disabled, window.localStorage should === null.

// Normally, we could not test that directly and need to do a
//   `('localStorage' in window) && ` test first because otherwise Firefox will
//   throw bugzil.la/365772 if cookies are disabled

// Also in iOS5 & Safari Private Browsing mode, attempting to use localStorage.setItem
// will throw the exception:
//   QUOTA_EXCEEDED_ERRROR DOM Exception 22.
// Peculiarly, getItem and removeItem calls do not throw.

// Because we are forced to try/catch this, we'll go aggressive.

// Just FWIW: IE8 Compat mode supports these features completely:
//   www.quirksmode.org/dom/html5.html
// But IE8 doesn't support either with local files

Modernizr.addTest('localstorage', function() {
    var mod = 'modernizr';
    try {
        localStorage.setItem(mod, mod);
        localStorage.removeItem(mod);
        return true;
    } catch(e) {
        return false;
    }
});
Run Code Online (Sandbox Code Playgroud)

使用当前源代码更新

  • 在try-catch块之前添加`var mod ="test";`,否则这将始终失败,因为mod在那里未定义. (3认同)
  • 如果localstorage中没有空格,则可能会失败. (3认同)
  • 请注意,这将在其他窗口中触发存储事件 (2认同)
  • @Andreas-是的,我知道。此测试将修改本地存储。如果您不希望这种情况发生,而只想测试支持,则其他解决方案可能是更好的选择。 (2认同)

小智 40

if(typeof Storage !== "undefined")
  {
  // Yes! localStorage and sessionStorage support!
  // Some code.....
  }
else
  {
  // Sorry! No web storage support..
  }
Run Code Online (Sandbox Code Playgroud)

  • `typeof(存储)!== void(0)`是更好的解决方案 (7认同)
  • `typeof`不是一个函数,它是一个语言运算符.为什么括号? (4认同)
  • @petjato它实际应该是`if(Storage!== void(0))` (2认同)
  • Safari中的隐私浏览模式不支持写入localStorage,但对于该情况,此检查返回true.Andreas的解决方案处理了这个案例. (2认同)

jua*_*nra 16

这个功能很好用:

function supports_html5_storage(){
    try {
        return 'localStorage' in window && window['localStorage'] !== null;
    } catch(e) {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

资料来源:www.diveintohtml5.info


小智 15

此外,我不想使用Modernizr,因为我只使用了一些HTML5功能,加载一个大脚本不值得它只是为了检测对这些事情的支持.

要减少Modernizr文件大小,请在http://modernizr.com/download/上自定义文件以满足您的需求.仅有localStorage版本的Modernizr的版本为1.55KB.

  • 嗨,欢迎来到Stack Overflow!这听起来更像是应该是[评论](http://stackoverflow.com/privileges/comment),而不是原始问题的*答案*.我意识到你还没有评论,但达到50 [rep](http://stackoverflow.com/faq#reputation)并不困难或耗时. (3认同)

Dan*_*nte 8

试试window.localStorage!==undefined:

if(window.localStorage!==undefined){
    //Do something
}else{
    alert('Your browser is outdated!');
}
Run Code Online (Sandbox Code Playgroud)

你也可以使用typeof window.localStorage!=="undefined",但上面的陈述已经做到了

  • Safari可能有localStorage,但如果处于私有模式(QuotaExceededError)则抛出错误,所以tr/catch是最好的选择恕我直言 (2认同)

Ovi*_*rif 7

我没有在答案中看到它,但我认为很高兴知道你可以轻松地使用vanilla JS或jQuery进行这样简单的测试,而Modernizr帮助很多,有没有它的清洁解决方案.

如果你使用jQuery,你可以这样做:

var _supportsLocalStorage = !!window.localStorage
    && $.isFunction(localStorage.getItem)
    && $.isFunction(localStorage.setItem)
    && $.isFunction(localStorage.removeItem);
Run Code Online (Sandbox Code Playgroud)

或者,使用纯Vanilla JavaScript:

var _supportsLocalStorage = !!window.localStorage
    && typeof localStorage.getItem === 'function'
    && typeof localStorage.setItem === 'function'
    && typeof localStorage.removeItem === 'function';
Run Code Online (Sandbox Code Playgroud)

然后,您只需执行IF来测试支持:

if (_supportsLocalStorage) {
    console.log('ls is supported');
    alert('ls is supported');
}
Run Code Online (Sandbox Code Playgroud)

因此,整个想法是,无论何时需要JavaScript功能,您都应首先测试父对象,然后测试代码使用的方法.