Solved

localStorage javascript not keep state of var

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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)
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…

820 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