Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

localStorage javascript not keep state of var

Posted on 2014-02-06
4
Medium Priority
?
392 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 2000 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 2000 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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)
Suggested Courses

721 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