{"id":2208,"date":"2022-08-30T15:22:57","date_gmt":"2022-08-30T15:22:57","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/04\/html5-local-storage-error-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:22:57","modified_gmt":"2022-08-30T15:22:57","slug":"html5-local-storage-error-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/html5-local-storage-error-collection-of-common-programming-errors\/","title":{"rendered":"HTML5 Local Storage Error-Collection of common programming errors"},"content":{"rendered":"<p>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(&#8220;count&#8221;); no line after this will run. What is wrong here? If the user is new there will be no &#8220;count&#8221; in local storage but it should just return null not crash.<\/p>\n<pre><code>var count = 0;\nstr_count = localStorage.getItem(\"count\");\nif (str_count == null || str_count == \"null\")\n{\n    \/\/do something\n} \nelse \n{\n    count = parseInt(str_count);\n            count++;\n            localStorage.setItem(\"count\", count);\n\n}\n<\/code><\/pre>\n<p>}<\/p>\n<ol>\n<li>\n<p>Does the browser support localStorage &#8211; you could check the feature before you try to get the value:<\/p>\n<pre><code>var str_count = 0;\nif (window.localStorage) {\n    str_count = localStorage.getItem('count');\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-01-04 14:19:22. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>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(&#8220;count&#8221;); no line after this will run. What is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2208","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2208"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2208\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}