0

I am new to JavaScript, i did this simple code to check local Storage. I just expected this to keep on the storage after refreshing or reopening the file...

Here is my code:

function save()
{
   localStorage.setItem('test', 'sometext');
}

I call it through a button on HTML. I open it on the Web Inspector and it's there, but once I refresh it's gone. It should stay there right?

4

2 回答 2

1

对我来说看起来不错,我认为问题是当您刷新 Web Inspector 时没有正确显示它。

试试下面的测试用例

<html>
<head>
    <script type="text/javascript">
        function save(){
            localStorage.setItem('test', 'sometext');
        }
        function getItem(){
            alert(localStorage.getItem('test'))
        }
    </script>
</head>
<body>
    <button onclick="save()">Save</button>
    <button onclick="getItem()">Get</button>
</body>
</html>

单击save后刷新页面并单击以get进行验证。

演示:小提琴

于 2013-04-03T04:10:02.180 回答
0

是的,它应该留在那里。除非您file://在 Internet Exlporer 或旧版本的 Firefox 中使用 URL。这里有一个深入的分析——Firefox中的“localStorage”是否只有在页面在线时才有效?当直接从文件系统访问网站时,IE9 中的本地存储会失败。

于 2013-04-03T04:20:03.980 回答