Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Javascript For Loop not working in Execute Advanced Actions - Execute Javascript?

Community Beginner ,
May 17, 2015 May 17, 2015

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.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

People's Champ , May 17, 2015 May 17, 2015

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.

Local Storage - Dive Into HTML5

Translate
Community Beginner ,
May 17, 2015 May 17, 2015

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 17, 2015 May 17, 2015

I'm using Adobe Captivate 8.0.1

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
May 17, 2015 May 17, 2015

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.

Local Storage - Dive Into HTML5

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 17, 2015 May 17, 2015

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 17, 2015 May 17, 2015

Doh! just realised that you gave me a link telling me how to use Local Storage for Persistent data. 

thanks again,

Chris

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 21, 2015 Dec 21, 2015

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 19, 2016 Jul 19, 2016
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Help resources