工作这么久了,一直也没有写博客的习惯,一直想着手开始写博客,今算第一篇吧.

很早也很简单的一个问题localStorage在safari的隐私无痕模式下无法使用,现在各种浏览器包括各种安卓手机系统自带的浏览器都有这个功能,但是有些项目不得不用localstorage..

之前一直用网上大家几乎都在用的try{}catch{}这种方法.

function isLocalStorage() {
var testKey = 'test',
storage = window.localStorage;
try {
storage.setItem(testKey, 'testValue');
storage.removeItem(testKey);
return true;
} catch (error) {
return false;
}
}




今天又遇到处理这个问题的时候,不是很忙就想看看有没有更好的方法,查了一下资料,自己试了一下,确实好用.

if(window.Storage && window.localStorage && window.localStorage instanceof Storage){
alert("支持");
}else{
alert("不支持");
}

感觉这样稍微优雅一些.