HTML5 Local Storage Error-Collection of common programming errors
I am trying to check if it is the first time the user has been to my application. So I check if count in local storage is null, if it is then the user is a new user. However, the code below stops at str_count = localStorage.getItem(“count”); no line after this will run. What is wrong here? If the user is new there will be no “count” in local storage but it should just return null not crash.
var count = 0;
str_count = localStorage.getItem("count");
if (str_count == null || str_count == "null")
{
//do something
}
else
{
count = parseInt(str_count);
count++;
localStorage.setItem("count", count);
}
}
-
Does the browser support localStorage – you could check the feature before you try to get the value:
var str_count = 0; if (window.localStorage) { str_count = localStorage.getItem('count'); }