Solved

localStorage problem

Posted on 2014-03-21
3
205 Views
Last Modified: 2014-03-28
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...

localStorage.setItem('groups', JSON.stringify(groups));

Open in new window


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);
}

Open in new window


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);
}

Open in new window


I think this may be to do with the DOM lifecycle, but can't figure it out.  

PLEASE HELP!!!
0
Comment
Question by:nickmarshall
  • 2
3 Comments
 
LVL 13

Expert Comment

by:devlab2012
ID: 39944908
Do you have both the statements setItem and getItem in the same script file?

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:

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
    <script type="text/javascript" src="jquery-1.7.2.js"></script>
    <script language="javascript" type="text/javascript">

        var groups = "Test localStorage";
        localStorage.setItem('groups', JSON.stringify(groups));

        $( document ).ready(function() {
            //SaveList();
            ShowList();
        });

        function ShowList() {
            var groups = JSON.parse(localStorage.getItem('groups'));
            console.log(groups);
        }
    </script>
</head>

<body style="width:220px;">
This is HTML body.
</body>
</html>

Open in new window


NOTE: To test the above code, make sure that you correctly include the jquery file referenced in this code.
0
 
LVL 1

Accepted Solution

by:
nickmarshall earned 0 total points
ID: 39949690
I was resetting the localStorage at the beginning of my script - Removing this fixed the issue.
0
 
LVL 1

Author Closing Comment

by:nickmarshall
ID: 39961064
Fixed myself.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logiā€¦
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now