Skip to main content
Known Participant
January 9, 2018
Answered

javascript for saving data in browser

  • January 9, 2018
  • 2 replies
  • 2107 views

Please can you help me with javascript for saving data?

In program AC there is variable called Points.

I need javascript which set localStorage... and will remember score/value of variable Points

even the program is shut.

Thank you.

This topic has been closed for replies.
Correct answer Jeremy Shimmerman

Hello,

if you ment to change this (first code)

apPoints = capPoints + 1;

localStorage.setItem("lsPoints", capPoints);

 

by that (second code)

capPoints = parseInt(localStorage.getItem("lsPoints"));

capPoints = capPoints + 1;

localStorage.setItem("lsPoints", capPoints);

 

it doesn´t work, I think because what you write to me, can´t "get" something what is not "set", becuase it looked like it before. the value of lsPoints is NaN.

It worked but only if I let the first version (but it remebered the points, but after first success changed it to zero and started again to count), but lsPoints were set, so if I changed the code (the second), everything was perfect until I delete the lsPoint and wanted to start completly new.

But of course it is not godd functiion. When I tried to set lsPoints first and than use the second code it was the same after first succes start again from zero.


Yes sorry about that.  I thought I might have made a mistake with that code and realized it on the subway as I was heading home.  What you need to have in there is something that evaluates if lsPoints is null (i.e. has not yet been set). This way if someone hasn't been on the site yet, lsPoints will be set to 0, but if they are coming back it will remember the score. 

Try this code for the first time you reference the local storage. 

if(localStorage.getItem("lsPoints") == null){

localStorage.setItem("lsPoints", 0);

}else{

capPoints = parseInt(localStorage.getItem("lsPoints"));

}

2 replies

Jeremy Shimmerman
Participating Frequently
January 9, 2018

To set the variable use:

localStorage.setItem("variableName","variableValue");

 

To then retrieve the variable use:

localStorage.getItem("variableName");

Known Participant
January 9, 2018

Thank you I tried. 

localStorage.setItem("Points","0");

localStorage.getItem("points");

In browser (Debugger, storage) I see the variable name Points and the value is o.

(In program if there is success there is order increment Points by 1,)

but after success there is still o (zero) in the browser.

Jeremy Shimmerman
Participating Frequently
January 9, 2018

It all depends on how and where you are 'setting' and 'getting' the variables.  You also have 'Points' with a capital and 'points' with a lower case. I think variables are case-sensitive.

Lilybiri
Legend
January 9, 2018

Did you look iat:

Learn about the Common JavaScript interface for Adobe Captivate

Scroll down, there is an example of using Localstorage for another variable, but you can translate it to your user variable.