如何检查localStorage中值的重复

时间:2014-09-06 16:32:15

标签: javascript local-storage

我只想存储一次localStorage值。如果它存在localStorage,则应显示警告。

2 个答案:

答案 0 :(得分:1)

if(localStorage.getItem('item') != null){
   alert('in localStorage');
}
else{
   alert('not in localStorage');
}

答案 1 :(得分:1)

这很简单。

只需检查密钥是否存在于localStorage中,如果不存在则添加密钥:

if(localStorage["keyName"]){
    alert("The key is stored");
}else{
    localStorage["keyName"] = "value";
}