Solved

localStorage javascript not keep state of var

Posted on 2014-02-06
4
385 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

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…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

752 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