chromeStorage在chrome mobile android中为null [英] localStorage is null in chrome mobile android

查看:105
本文介绍了chromeStorage在chrome mobile android中为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用



然后你可以再次停止并运行plunkr,你会看到你的屏幕名称已经保存到localStorage并读出localStorage以填充顶部消息:





我实际上已经过测试这适用于各种版本的Android,iOS(在iPa上d)在各种桌面浏览器中,它适用于每种情况。



这是调用localStorage.setItem()的代码

  function writeScreeNameToStorage(screenName){
var encodedScreenName = getEncodedValue(screenName);
localStorage.setItem(screenName,encodedScreenName);
}


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);
}

这篇关于chromeStorage在chrome mobile android中为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆