Reputation: 131
I'm trying to implement local storage
for navigating by pages.
I have a web site with some pages, all in one folder.
In one page, I'm setting the local storage:
var myObject = { 'id': 1, 'price': 50.0, 'total': 3 };
localStorage.setItem('data', JSON.stringify(myObject));
While in the first page, I see the local storage with data- thats OK. But after navigating to the second page, I see that the local storage is empty while trying to read:
var localObject = localStorage.getItem('data');
Can't understand where is the problem.
Upvotes: 0
Views: 2177
Reputation:
You are not showing much of the code, but localStorage
won't throw a reference error if an item does not exist.
It does not matter if you navigated to another domain as the localStorage
would just return null
when using getItem
(undefined
if you used localStorage[key]
).
This indicates that you have a problem elsewhere in the code not directly related to localStorage
.
But you need to show a bit more code and tell us what line the error occur on.
Also as stated in the comments, you would need to JSON.parse
the data to re-create the object you stringify'ed.
Upvotes: 2