Javascript 如何检查是否设置了 Storage 项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3262605/
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
How to check whether a Storage item is set?
提问by Jiew Meng
How can I check if an item is set in localStorage? Currently I am using
如何检查一个项目是否被设置localStorage?目前我正在使用
if (!(localStorage.getItem("infiniteScrollEnabled") == true || localStorage.getItem("infiniteScrollEnabled") == false)) {
// init variable/set default variable for item
localStorage.setItem("infiniteScrollEnabled", true);
}
回答by CMS
The getItemmethod in the WebStorage specification, explicitly returns nullif the item does not exist:
getItemWebStorage 规范中的方法,null如果该项不存在,则显式返回:
... If the given key does not exist in the list associated with the object then this method must return null. ...
...如果给定的键不存在于与对象关联的列表中,则此方法必须返回 null。...
So, you can:
这样你就可以:
if (localStorage.getItem("infiniteScrollEnabled") === null) {
//...
}
See this related question:
看到这个相关的问题:
回答by Stephan Hoyer
You can use hasOwnPropertymethod to check this
您可以使用hasOwnProperty方法来检查这个
> localStorage.setItem('foo', 123)
undefined
> localStorage.hasOwnProperty('foo')
true
> localStorage.hasOwnProperty('bar')
false
Works in current versions of Chrome(Mac), Firefox(Mac) and Safari.
适用于当前版本的 Chrome(Mac)、Firefox(Mac) 和 Safari。
回答by Vladislav
The shortest way is to use default value, if key is not in storage:
最短的方法是使用默认值,如果键不在存储中:
var sValue = localStorage['my.token'] || ''; /* for strings */
var iValue = localStorage['my.token'] || 0; /* for integers */
回答by Prime_Coder
You can also try this if you want to check for undefined:
如果你想检查未定义,你也可以试试这个:
if (localStorage.user === undefined) {
localStorage.user = "username";
}
getItem is a method which returns null if value is not found.
getItem 是一种如果未找到值则返回 null 的方法。
回答by Deepak Thomas
if(!localStorage.hash) localStorage.hash = "thinkdj";
Or
或者
var secret = localStorage.hash || 42;
回答by Derin
For TRUE
为真
localStorage.infiniteScrollEnabled = 1;
FOR FALSE
假的
localStorage.removeItem("infiniteScrollEnabled")
CHECK EXISTANCE
检查存在
if (localStorage[""infiniteScrollEnabled""]) {
//CODE IF ENABLED
}
回答by le vantard
How can one test existence of an item in localStorage?
this method works in internet explorer.
如何测试一个项目在 中的存在localStorage?此方法适用于 Internet Explorer。
<script>
try{
localStorage.getItem("username");
}catch(e){
alert("we are in catch "+e.print);
}
</script>
回答by webmaster
You should check for the typeof the item in the localStorage
您应该检查localStorage 中项目的类型
if(localStorage.token !== null) {
// this will only work if the token is set in the localStorage
}
if(typeof localStorage.token !== 'undefined') {
// do something with token
}
if(typeof localStorage.token === 'undefined') {
// token doesn't exist in the localStorage, maybe set it?
}
回答by Gaurav Gandhi
Best and Safest way i can suggest is this,
我能建议的最好和最安全的方法是这个,
if(Object.prototype.hasOwnProperty.call(localStorage, 'infiniteScrollEnabled')){
// init variable/set default variable for item
localStorage.setItem("infiniteScrollEnabled", true);
}
This passes through ESLint's no-prototype-builtinsrule.
这通过了 ESLint 的no-prototype-builtins规则。
回答by Rahul Vanave
I've used in my project and works perfectly for me
我在我的项目中使用过并且非常适合我
var returnObjName= JSON.parse(localStorage.getItem('ObjName'));
if(returnObjName && Object.keys(returnObjName).length > 0){
//Exist data in local storage
}else{
//Non Exist data block
}

