Skip to main content
Nathan Grieve
Known Participant
July 4, 2016
Answered

JavaScript disappears in advanced action.

  • July 4, 2016
  • 3 replies
  • 870 views

Don't know why this is happening, but when I try to execute JavaScript in an advanced action, the JavaScript disappears in the script window before. I've attached the script below (basically assigns random numbers to different variables)

Any help would be appreciated. Thanks

var numArray = [1,2,3];

var numStr

var i = 1;

var arrSize = numArray.length + 2;

while (i<arrSize){

getRandom();

i++;

}

function getRandom() {

var myMax = numArray.length

var myArrRand = Math.floor(Math.random() * (myMax - 0)) + 0; // random place in array to use

var myRand = numArray[myArrRand] // get element from array

  var assignedNumber = 'ShowAnsBox' + i;

                window.cpAPIInterface.setVariableValue(assignedNumber, myRand);

  numArray.splice(myArrRand,1);

}

This topic has been closed for replies.
Correct answer TLCMediaDesign

In my experience this happens when Cp cannot escape the code correctl. Certain characters will break the code when converted to a string. Your best bet is to put the code in an external js file or in the head of the index.html.

3 replies

Known Participant
August 16, 2017

Has anyone ever found an answer for this?? I'm having the same trouble. My code will work in a simple action, where you just choose execute javascript from the drop down, but in advanced actions the code keeps disappearing.

Legend
August 16, 2017

TLCMediaDesign's response still holds true and I'd 2nd that recommendation.  If you have a lot of complex JavaScript, the code editor will not escape it properly.  Regular Expressions have been problematic for me in the past.  Trying to find the offending JS statement is trial and error.  The easiest way is to put your code in an external js file.

TLCMediaDesign
TLCMediaDesignCorrect answer
Inspiring
August 13, 2016

In my experience this happens when Cp cannot escape the code correctl. Certain characters will break the code when converted to a string. Your best bet is to put the code in an external js file or in the head of the index.html.

Participant
August 12, 2016

Similar problem here.

If I enter a short script (not more than about 2 -5 lines of code) everything works fine.

But when I enter a longer script (about 1.5 times as large as the scripting window)

then the script is not saved.

To be more precise: When I close the scripting window with "OK" and then reenter the scripting window then the whole script is gone.

There are no error messages.

I'm running Captivate 9.0.1 on Windows in a virtual box on Ubuntu Linux.

It only happens in advanced actions, in simple actions I can enter large scripts.