nickmarshall
asked on
localStorage problem
Hi,
I have a problem with accessing the localStorage values in my HTML5 application.
I am setting the localStorage key, groups at the top of my javascript file to an object as follows...
Within the document.ready part of my script I attempt to retrieve the value as follows...
Inspecting the Firebug console shows, undefined for the log. However, if I place an alert("HELLO") before groups is defined, the localStorage object is returned correctly...
I think this may be to do with the DOM lifecycle, but can't figure it out.
PLEASE HELP!!!
I have a problem with accessing the localStorage values in my HTML5 application.
I am setting the localStorage key, groups at the top of my javascript file to an object as follows...
localStorage.setItem('groups', JSON.stringify(groups));
Within the document.ready part of my script I attempt to retrieve the value as follows...
function ShowList() {
var groups = JSON.parse(localStorage.getItem('groups'));
console.log(groups);
}
Inspecting the Firebug console shows, undefined for the log. However, if I place an alert("HELLO") before groups is defined, the localStorage object is returned correctly...
function ShowList() {
alert("HELLO");
var groups = JSON.parse(localStorage.getItem('groups'));
console.log(groups);
}
I think this may be to do with the DOM lifecycle, but can't figure it out.
PLEASE HELP!!!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Fixed myself.
It might be the order in which you are using the javascript files. In general, localStorage is available in the document.ready and you may use it without any issue. Following is a simple HTML page, that uses the same code as listed by you:
Open in new window
NOTE: To test the above code, make sure that you correctly include the jquery file referenced in this code.