Jak sprawdzić, czy Element magazynu jest ustawiony?

Jak mogę sprawdzić, czy Element jest ustawiony w localStorage? Obecnie używam

if (!(localStorage.getItem("infiniteScrollEnabled") == true || localStorage.getItem("infiniteScrollEnabled") == false)) {
    // init variable/set default variable for item
    localStorage.setItem("infiniteScrollEnabled", true);
}
Author: Xufox, 2010-07-16

9 answers

The getItem metoda w specyfikacji WebStorage, jawnie zwraca null jeżeli element nie istnieje:

... Jeżeli dany klucz nie istnieje na liście skojarzonej z obiektem, wtedy metoda ta musi zwrócić null. ...

Więc możesz:

if (localStorage.getItem("infiniteScrollEnabled") === null) {
  //...
}

Zobacz to pytanie:

 359
Author: CMS,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:10:44

Najkrótszym sposobem jest użycie wartości domyślnej, jeśli klucz nie jest przechowywany:

var sValue = localStorage['my.token'] || ''; /* for strings */
var iValue = localStorage['my.token'] || 0; /* for integers */
 14
Author: Vladislav,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-20 12:59:43

Możesz użyć metody hasOwnProperty, aby to sprawdzić

> localStorage.setItem('foo', 123)
undefined
> localStorage.hasOwnProperty('foo')
true
> localStorage.hasOwnProperty('bar')
false

Działa w aktualnych wersjach Chrome( Mac), Firefox(Mac) i Safari.

 12
Author: Stephan Hoyer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-04 11:16:02

Możesz również spróbować, jeśli chcesz sprawdzić, czy nie jest niezdefiniowany:

if (localStorage.user === undefined) {
    localStorage.user = "username";
}

GetItem jest metodą, która zwraca null, jeśli nie znaleziono wartości.

 4
Author: Prime_Coder,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-29 12:10:03

Jak sprawdzić istnienie elementu w localSotorage? ten działa w internet Explorerze

<script>
    try{
        localStorage.getItem("username");
    }catch(e){
        alert("we are in catch "+e.print);
    }
</script>
 2
Author: le vantard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-08 18:05:37

For TRUE

localStorage.infiniteScrollEnabled = 1;

DLA FALSE

localStorage.removeItem("infiniteScrollEnabled")

SPRAWDŹ ISTNIENIE

if (localStorage[""infiniteScrollEnabled""]) {
  //CODE IF ENABLED
}
 1
Author: Derin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-22 04:33:47
if(!localStorage.hash) localStorage.hash = "thinkdj";

Lub

var secret =  localStorage.hash || 42;
 1
Author: Deepak Thomas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-10 20:49:09

Należy sprawdzić typ elementu w 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?
}
 1
Author: webmaster,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-24 15:21:12
localStorage['root2']=null;

localStorage.getItem("root2") === null //false
Może lepiej zrobić skan planu ?
localStorage['root1']=187;
187
'root1' in localStorage
true
 0
Author: zloctb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-06-26 07:35:44