Reputation: 11
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?
Upvotes: 0
Views: 146
Reputation: 1977
Yes, it should stay there. Unless you are using file://
URL in Internet Exlporer or older version of Firefox.
There is an in-depth analysis here - Is “localStorage” in Firefox only working when the page is online? and here - local storage in IE9 fails when the website is accessed directly from the file system.
Upvotes: 0
Reputation: 388436
It looks fine to me, I think the problem is when you refresh the Web Inspector is not showing it properly.
Try the following test case
<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>
Once you click on save
then refresh the page and click on get
to verify.
Demo: Fiddle
Upvotes: 1