Why isn't localStorage persisting in Chrome?

后端 未结 2 1077
孤城傲影
孤城傲影 2020-12-29 05:52

I\'m trying to learn how to use the localStorage js object with the following code.






        
相关标签:
2条回答
  • 2020-12-29 06:33

    <!-- Necro post I know, but maybe it helps someone else -->

    I couldn't figure out why I couldn't see something in my localStorage in Dev Tools, but could see it in the console when typing localStorage. Well, if you store something with Dev Tools open, you won't see Application -> Local Storage have anything listed for your host. When I closed/reopened Dev Tools, I could see my data.

    So, in conclusion, close/reopen Dev Tools.

    0 讨论(0)
  • 2020-12-29 06:42

    Ok. Thanks must go to pimvdb on this one but here's the solution.

    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript">
    function saveStuff() {
        sessionStorage.setItem('sessionKey', 'sessionValue');
        localStorage.setItem('localKey', 'localValue');
    }
    localStorage.getItem('localKey');
    sessionStorage.getItem('sessionKey');
    </script>
    </head>
    <body>
    <button type="button" onclick="saveStuff()">Save</button>
    </body>
    </html>
    

    Summary seems to be that that you must attempt a read of the key in order to persist it.

    Conclusion: WTF.

    0 讨论(0)
提交回复
热议问题