Copy link to clipboard
Copied
Hi
First things first. I don't know the J of JavaScript.
When I was thinking of some readymade solution to generate a random number in Captivate, I found this on a LinkedIn forum.
1) Add a user variable to your project called "randomNumber". It's value can be zero.
2) Add a Text Caption with "$$randomNumber$$" in it, to check whether your script is working.
3) Add a button to the slide. Give it an On Success Action of Execute JavaScript. Turn off it's "Continue Playing the Project" checkbox.
4) Add the following text in the button's Script_Window:
var objCP = document.Captivate;
var rand = 1 + Math.floor(Math.random() * 10);
function onButtonClick(){
objCP.cpEISetValue('randomNumber', rand);
}
onButtonClick();
However, this is (as the poster in the forum warned) not working for Captivate 7. Can somebody please help?
I am open for any other methods of getting the same result.
Thanks in advance,
Sreekanth
1 Correct answer
Hi Sreekanth,
When testing, make sure you are testing from a web server where the web address begins with http or https. When setting the captivate variable "randomNumber" try this instead:
objCP.cpEISetValue('m_VarHandle.randomNumber', rand);
That will only work for SWF output. If you want it to work for both HTML5 and SWF output, try this code:
...window.onButtonClick = function(){
var rand = generateRandomNumber(1, 10);
setCpVariable('randomNumber', rand);
};
window.generateRandomN
Copy link to clipboard
Copied
Hi Sreekanth,
When testing, make sure you are testing from a web server where the web address begins with http or https. When setting the captivate variable "randomNumber" try this instead:
objCP.cpEISetValue('m_VarHandle.randomNumber', rand);
That will only work for SWF output. If you want it to work for both HTML5 and SWF output, try this code:
window.onButtonClick = function(){
var rand = generateRandomNumber(1, 10);
setCpVariable('randomNumber', rand);
};
window.generateRandomNumber = function(min, max){
var randomNum = 0;
if(!isNaN(parseFloat(min)) && !isNaN(parseFloat(max))){
min = Number(min);
max = Number(max);
randomNum = Math.floor(Math.random() * (max - min + 1)) + min;
}
return randomNum;
};
window.setCpVariable = function (cpUserVariableName, variableValue) {
/* Check for HTML5 vs. SWF output */
if (typeof window.cp === 'undefined') {
/* We have SWF output, so Get the Captivate Object */
var objCp = document.getElementById('Captivate');
if (objCp && objCp.cpEISetValue) {
/* Set the Captivate User variable with the JavaScript variable, variableValue */
objCp.cpEISetValue('m_VarHandle.' + cpUserVariableName, variableValue);
}
} else {
/* We have HTML5 output */
/*If variable does not exist off of the window object, then use the variables manager*/
if (typeof window[cpUserVariableName] === 'undefined') {
if (cp.vm && cp.vm.setVariableValue) {
cp.vm.setVariableValue(cpUserVariableName, variableValue);
}
} else {
window[cpUserVariableName] = variableValue;
}
}
};
window.onButtonClick();
Be sure to test this from a web server or else local security will prevent the javascript from executing.
Best,
Jim Leichliter
Copy link to clipboard
Copied
Guess what Jim, couple of hours back I had read your blog post on Javascript in Captivate 6 and I had tried with the same modification as you mentioned in your blog post. But it failed.
Now, I restarted my laptop and made this modification on a new Captivate project and it works fine. In fact, it works in the preview itself.
Thanks a ton.
Sreekanth
Copy link to clipboard
Copied
My pleasure Sreekanth. I wish you success on your project!
Best,
Jim Leichliter

