javascript IE8 和 localStorage 支持
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15313606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IE8 and localStorage support
提问by Test Test
I am not sure if IE8 fully supports localStorage. But I use the following method to detect
我不确定 IE8 是否完全支持 localStorage。但是我用下面的方法来检测
function supports_html5_storage()
{
try {
return 'localStorage' in window && window['localStorage'] !== null;
}
catch (e) {
return false;
}
}
Now IE returns true for 'localStorage' in window
现在 IE 在窗口中为“localStorage”返回 true
But returns undefined for window['localStorage']
但为 window['localStorage'] 返回 undefined
So should I update this method OR does IE8 indeed have local storage support ?
那么我应该更新这个方法还是 IE8 确实有本地存储支持?
回答by kennebec
You can try to set and read localStorage.
您可以尝试设置和读取localStorage。
Some browsers return a security error if cookies are disabled or you are working with file: protocol.
如果禁用 cookie 或您正在使用 file: 协议,某些浏览器会返回安全错误。
function hasStorage(){
try{
localStorage.setItem('test', '7');
if(localStorage.getItem('test')=== '7'){
localStorage.removeItem('test');
return true;
}
}
catch(er){}
return false;
}
alert(hasStorage())
警报(hasStorage())
回答by prakashapkota
Here is a famous localStorage plugin https://github.com/marcuswestin/store.js/you can add, edit and delete datas very easily and the most important is you can use localstorage in IE6+.
这里有一个著名的localStorage 插件 https://github.com/marcuswestin/store.js/你可以很容易地添加、编辑和删除数据,最重要的是你可以在IE6+中使用localstorage。
store.js uses localStorage when available, and falls back on the userData behavior in IE6 and IE7. No flash to slow down your page load. No cookies to fatten your network requests. Ex :
store.js 在可用时使用 localStorage,并依靠 IE6 和 IE7 中的 userData 行为。没有闪光灯来减慢您的页面加载速度。没有 cookie 来增加您的网络请求。前任 :
store.set('user', { name: 'marcus', likes: 'javascript' })