Solved

localStorage javascript not keep state of var

Posted on 2014-02-06
4
375 Views
Last Modified: 2016-03-24
Hi,

I can't get the localStorage to work when the user clicks on the Paypal button and then later clicks the browser's back button.

$(document).ready(function() {
aInputDataVals['test'] = 2; 
localStorage.setItem( 'inputDataLocalStorage', JSON.stringify(aInputDataVals) );
oInputDataLocalStorage = JSON.parse(localStorage['inputDataLocalStorage']);
});

Open in new window


this works.

During the page here, the user can select some checkboxes which gets added to the object:

aInputDataVals['checkbox1'] = '5';
localStorage.setItem( 'inputDataLocalStorage', JSON.stringify(aInputDataVals) );
oInputDataLocalStorage = JSON.parse(localStorage['inputDataLocalStorage']);

Open in new window


I can see the oInputDataLocalStorage. Everything looks good so far.

But when the user clicks on a Paypal and then clicks the browser's back button, the $(document).ready(function()) gets reloaded and I no longer see the last key:value added:
aInputDataVals['checkbox1'] = '5';

in this var oInputDataLocalStorage.

It's a global var right now.

Thank you.
0
Comment
Question by:Victor Kimura
  • 2
  • 2
4 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 500 total points
ID: 39840445
Set a value in localstorage that you can check if the page has already been visited, and if not set then fire your localstorage code.
0
 

Author Comment

by:Victor Kimura
ID: 39840489
Hi Cathal,

Thanks, not quite sure what you mean by "Set a value in localstorage that you can check if the page has already been visited"

or by "if not set then fire your localstorage code."

Do you have some code example you're thinking of to illustrate? =)
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39840528
Something like this

$(document).ready(function() {
    if(localStorage.getItem( 'pageVisited') !='1'){
        localStorage.setItem( 'pageVisited', '1')
        aInputDataVals['test'] = 2; 
        localStorage.setItem( 'inputDataLocalStorage', JSON.stringify(aInputDataVals) );
        oInputDataLocalStorage = JSON.parse(localStorage['inputDataLocalStorage']);
    }
}); 

Open in new window

0
 

Author Comment

by:Victor Kimura
ID: 39840681
Oh, yes, I see, Cathal. Didn't think of the checking with the if statement too. Ok, thanks so much for the code and insight. =)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 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

19 Experts available now in Live!

Get 1:1 Help Now