Solved

localStorage javascript not keep state of var

Posted on 2014-02-06
4
378 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

803 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