Copy link to clipboard
Copied
I am using a For loop to check the value of a Cookie. When I add this using Execute Javascript it works fine but I want it to run with some other actions so I want to use Execute Advanced Actions. When I add this coded to an Execute Javascript action within Execute Advanced Actions the code has disapeared if I open the Script Window again. I try removing the for loop code in Bold below then the remaining code stays. So I think that the problem must be with the for loop but I don't see anything wrong with this code and I as I say this code works fine if I use Execute Javascript on it's own. I've attached a video clip below to show what I mean.
var task1_value = readCookie("task1");
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0; i < ca.length; i++) {
var c = ca;
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
1 Correct answer
I've found that CP's JavaScript window doesn't like loops.
Your best bet is to put the function in an external JS file of in the head of the page. I always run scripts in an external file. Really helps debugging as you don't have to republish Captivate. Just change your code and refresh the browser. If you use and external file you can store all of your functions there.
I would also look into using local storage instead of cookies. Very easy to implement and you can store a ton of information.
Copy link to clipboard
Copied
I narrowed this down further it seems that it is specifically the < symbol that it doesn't like. If you include a < symbol in script window for. Execute Advanced Actions - Execute Javascript the code is removed.The > symbol is fine. See attached video below.
Copy link to clipboard
Copied
I'm using Adobe Captivate 8.0.1
Copy link to clipboard
Copied
I've found that CP's JavaScript window doesn't like loops.
Your best bet is to put the function in an external JS file of in the head of the page. I always run scripts in an external file. Really helps debugging as you don't have to republish Captivate. Just change your code and refresh the browser. If you use and external file you can store all of your functions there.
I would also look into using local storage instead of cookies. Very easy to implement and you can store a ton of information.
Copy link to clipboard
Copied
Thanks very much TLCMeidaDesign. That did the trick. I am using the cookies to track progress and I want progress to be remembered after the user leaves the program so I don't think that I can use local storage for this? Or can I?
Copy link to clipboard
Copied
Doh! just realised that you gave me a link telling me how to use Local Storage for Persistent data.
thanks again,
Chris
Copy link to clipboard
Copied
Thought I would reply, even though a solution has been provided. I had the same problem and your clue re the > sign working gave me the solution to using the loops in the Execute JavaScript action. I thought I would share it in case others wanted the solution.
Basically, count backwards.
var total = 0;
for(i=5; i>0; i--){
total+=i;
}
total = 15
Copy link to clipboard
Copied
I'm so glad I found this thread. I was pulling my hair out, never would've thought it was the < sign.
Some other alternatives:
for ( i = 5; 5 > i; i++) { /*code*/ }
for ( i = 5; i != 5; i++) { /*code*/ }
Both of these ways let me save the Script Window in an Advanced Action.

