I use Sentry to track the error in my website. The problem is sometimes chrome mobile for android has a null localStorage that cause all related method such as getItem or setItem will produce an error (Yeah, it's flaky). Have you ever got the problem like this before? I'd like to know the best solution for this problem.
Thanks :)
Maybe it is because this question is almost 2 years old and they've changed things but localStorage definitely works in Android Chrome with localStorage.setItem() and getItem() calls.
There is no need for the polyfill.
However, there is a problem / challenge that occurs if you've already put a page out there and you add localStorage that is related to Chrome Caching behavior. That's the problem I was having.
I had a page on the web than did some things via JavaScript. Then I added the simple localStorage calls to write and read a string value. I found that localStorage would fail.
Finally I loaded the .js file directly in my Android chrome browser and saw that the JS was the cached version of the file.
I reloaded the JS file and saw it update in Android Chrome so I went back to the page that uses the JS file and it still didn't save to localStorage.
To get it to work I had to close Android Chrome entirely (after refreshing the JS file) and finally localStorage did indeed work.
Here's a plunkr where you can point your mobile device at and save a value in localStorage to prove to yourself that it works.
http://plnkr.co/edit/dWgfh5WuMcM3InR0OaN4?p=info
Type in a screen name and press the Save button:
Then you can Stop and Run the plunkr again and you'll see that your Screen Name has been saved to localStorage and read out of localStorage to populate the top message:
I've actually tested this on various versions of Android, iOS (on an iPad) and in various desktop browsers and it works in every case.
Here's the code that calls localStorage.setItem()
function writeScreeNameToStorage(screenName){
var encodedScreenName = getEncodedValue(screenName);
localStorage.setItem("screenName", encodedScreenName);
}