ls exist
IE7中的以下代码警报:
if(window.localStorage) {
alert('ls exists');
} else {
alert('ls does not exist');
}
IE7并不真正支持本地存储,但是仍然可以警告它。也许这是因为我正在IE7浏览器中使用IE9,并使用IE9开发人员工具在文档模式下使用。也许这只是测试LS是否受支持的错误方法。正确的方法是什么?
另外,我也不想使用Modernizr,因为我仅使用了几个HTML5功能,并且仅加载单个脚本来检测对这几件事的支持是不值得的。
您不必使用modernizr,但可以使用其方法来检测是否localStorage
受支持
github上的modernizr
测试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;
}
});
用当前源代码更新
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句