Copy link to clipboard
Copied
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.
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.setIte
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
To set the variable use:
localStorage.setItem("variableName","variableValue");
To then retrieve the variable use:
localStorage.getItem("variableName");
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Yes, but Points and points is problem of writting here, not in program.
javascript is on the enter of the first slide, where is first exercise.
And in case of the success of this same slide there is another variable:
increment Points by 1
Continue.
Do you think the problem is that it is hotspot slide?
Copy link to clipboard
Copied
The problem is not that it is a hotspot slide. I would need to see how you have it set up to really understand what's happening. I suspect that your variable keeps 'resetting' on reload because you are declaring the value somewhere, or that it is not set properly.
Try this:
1. Create a variable in Captivate under project called capPoints. Give it a value of 0.
2. Each time capPoints is changed (incremented or decremented) execute this js code:
capPoints = capPoints + 1;
localStorage.setItem("lsPoints", capPoints);
3. On your execute javascript when page loads window use this:
capPoints = parseInt(localStorage.getItem("lsPoints"));
Copy link to clipboard
Copied
You are really great, I appreciate your kidness, but it doesn´t work.
I would like to show you, how it is set up (maybe by screenshot but I´m not sure if I can do this there), so would it be possible by mail?
Copy link to clipboard
Copied
You can take a screen shot and then click 'add media' to the message. If you prefer, you could add the .cptx file and I can see what is happening.
Copy link to clipboard
Copied
Fine, here it is.
So I changed it little bit, because it would take you a lot of time in origin vesion. (it is set up, that you do 10 examples and if you get 9 or more points you will get reward (and reward points must be saved in browser, there would be JS)). But I changed it that you will get reward points after every example, because you needn´t passo all exercise)
So first JS is On enter od slide 3, and than on slide 4 when you click on Sumbit and it will be Success. There are some other other in Advanced action (they must be).
Copy link to clipboard
Copied
Ok I took a look through the file and here is what I found. I did manage to get the local storage to work but there are some other issues that are interfering with it.
Here is what you need to change:
1. On slide VOD_1 delete the javascript on enter frame. You cannot 'get' something from local storage if it has not yet been set.
2. Your advanced actions for success 'VO_Dvojice_body' is not accurate and I think this is messing up the storage. You are combining your conditionals. The script should be
IF - counting is equal to 10
ACTION - jump to slide 35Konec
ELSE (make sure you click the bottom else button - people miss it
) - Increment Body by 1,
Execute Javascript: capPoints = capPoints + 1;
localStorage.setItem("lsPoints", capPoints);
Continue
If you hit f12 and type into the console 'localStorage.lsPoints' (no quotes) you will see the numbers increment. If you also type in 'capPoints' (no quotes) you should see the same number.
_____________
As far as the rewards points, you need to figure out what happens when capPoints / lsPoints reaches whatever number you want, and what you want to happen.
On each slide (after you 'set' lsPoints) you can create a script such as:
capPoints = parseInt(localStorage.getItem("lsPoints"));
if (capPoints == 10){
DO SOME ACTION
}
I hope this helps you out.
Copy link to clipboard
Copied
Thank you very much, I will try it and hope it will work.
And if it will I think you will deserve reward (but I prefer cryptocurrency...)
Copy link to clipboard
Copied
No problem. Let me know if it works and if you have any other questions. All the best,
Copy link to clipboard
Copied
Hello,
yes, it works! Great, but maybe because English it is not ma native languague
there is little bit misunderstanding. So I apologise to you for wasting your time.
When I shut the program and open it again, it remembers the reward points
(for example it was 6), but after first slide there is again 1. But I need it to be 7.
Copy link to clipboard
Copied
I think what is happening is that you are telling Captivate to give capPoints a value of 0 when the program loads and it resets.
When executing the javascript try this code instead.
capPoints = parseInt(localStorage.getItem("lsPoints"));
capPoints = capPoints + 1;
localStorage.setItem("lsPoints", capPoints);
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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"));
}
Copy link to clipboard
Copied
Yes, this code (condition) seems to be right!
I would like to ask you for some things.
1) capPoints and lsPoints are the same? Mean the localstorage remembers both?
2) Is it possible to set more capPoints or lsPoints in one browser (mean one block with 3 exercise would reffer to lsPoints_1, another block to lsPoints_2...) and will javascript accept more conditions, i.e. if (lsPoints_1 ==1) and (lsPoints_2 ==1) {DO SOMETHING}?
3) is it possible to disable button in Adobe Captivate by setting code in javascript?
Copy link to clipboard
Copied
Glad it was workings. (I might end up making a blog post about localstorage). To answer your questions.
1. No. No. When the line capPoints = parseInt(localStorage.getItem("lsPoints")) is called, it will change capPoints's value to be the same as lsPoints. localStorage is only storing lsPoints (I used 'ls' to stand for Local Storage so we wouldn't get confused). So capPoints and lsPoints are two variables, one stored in localStorage, the other 'resets' each time Captivate restarts.
2. Yes. You can have as many variables (declared in captivate or in localStorage as you want). Yes. You can have conditionals that evaluate all variables (locally stored or not). The syntax for that is
if(localStorage.lsPoints_1 == 1 && localStorage.lsPoints_2 == 1 && capPoints == 2) { DO SOMETHING}
3. Yes. cp.disable('ID')
Copy link to clipboard
Copied
Hello Jeremy,
one thing yet. If lsPoints is other variable can I show it in AdobeCaptivate?
To explain, if I want to show user how many points he gains I need to insert variabile to Captivate. I can use capPoints which I create, but lsPoints is stored in localStorage,
but I don´t want users go to local storage, it shloud work only like some kond of "memory".
So how can I insert it into AC?
Copy link to clipboard
Copied
When you are ready to show the users their points, you would need to change the value of capPoints to equal the same as lsPoints.
capPoints = parseInt(localStorage.getItem("lsPoints"));
On the slide, you would need a textbox that shows the variable capPoints.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more