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

How to read Captivate variables in external JavaScript

New Here ,
Mar 18, 2016 Mar 18, 2016

Hello,

I want to develop combination of quiz questions and test me simulations in Captivate. And send the results to external JavaScript so I can save them to the Database.

I published the Captivate to HTML5, typed this code in an external .js file and linked it to index.html, but I'm only getting the alert "no CPI interface" as the interface is not detected I think. I'm testing it from a local web server. Any ideas?

==============================================================================

var interfaceObj;

var eventEmitterObj;

window.addEventListener("moduleReadyEvent", function(evt)

{

    //evt.Data carries the interface object.

    //It is same as window.cpAPIInterface

    interfaceObj = evt.Data;

    eventEmitterObj = interfaceObj.getEventEmitter();

});

//check if window.cpAPIInterface is available

if(interfaceObj)

{

    //check if window.cpAPIEventEmitter is available

    if(eventEmitterObj)

    {

        //add a listener to CPAPI_SLIDEENTER event

        eventEmitterObj.addEventListener("CPAPI_SLIDEENTER",function(e)

                                                                    {

                                                                        var cAns = interfaceObj.getVariableValue("slideNumber");   

                                                                        alert(cAns);

                                                                        //window.cpAPIInterface.setVariableValue("mySlideVisitCounter",lSlideVisitCtr);

                                                                    });

    }

}else{

    alert("No CPI interface")

}

TOPICS
Advanced
2.4K
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 , Mar 30, 2016 Mar 30, 2016

The data you are speaking of is in the event, not the window.

eventEmitterObj.addEventListener( 'CPAPI_QUESTIONSUBMIT', function ( e ){ 

var cAns = e.Data.correctAnswer;  

alert("correctAnswer="+cAns);

Translate
People's Champ ,
Mar 18, 2016 Mar 18, 2016

The  interfaceObj is not the same as the cpAPIInterface object. All of the Captivate variable are in the window object. Try this:

var interfaceObj, eventEmitterObj;

window.addEventListener( 'moduleReadyEvent', function ( e )
{
interfaceObj = e.Data;
eventEmitterObj = interfaceObj.getEventEmitter();
    initializeEventListeners();
});

function initializeEventListeners()
{
if ( interfaceObj )
{
     if ( eventEmitterObj )
  {
         eventEmitterObj.addEventListener( 'CPAPI_SLIDEENTER', function ( e )
   { 
     var cAns = window.slideNumber;   

                  alert(cAns);
      //window.cpAPIInterface.setVariableValue("mySlideVisitCounter",lSlideVisitCtr);

            });  
  }
}
}

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 ,
Mar 28, 2016 Mar 28, 2016

Hi TLCMediaDesign, Thanks for the solution. I tried it but it returns an undefined value.

eventEmitterObj.addEventListener( 'CPAPI_QUESTIONSUBMIT', function ( e ){ 

var cAns = window.correctAnswer;  

alert("correctAnswer="+cAns);

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 ,
Mar 29, 2016 Mar 29, 2016

What are you using to populate correctAnswer?

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 ,
Mar 29, 2016 Mar 29, 2016

I'm using the Captivate event data "correctAnswer" as listed on the Adobe URL Common JS interface . So the event is CPAPI_QUESTIONSUBMIT and event data is "correctAnswer".  Do I need to separately define this variable in Captivate? I thought it will be automatically populating values from the quiz.

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 ,
Mar 29, 2016 Mar 29, 2016

I have also tried populating a custom variable and then read that variable in Javascript. But it returns an empty value. If I publish for SCORM then it returns "undefined".

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 ,
Mar 30, 2016 Mar 30, 2016

The data you are speaking of is in the event, not the window.

eventEmitterObj.addEventListener( 'CPAPI_QUESTIONSUBMIT', function ( e ){ 

var cAns = e.Data.correctAnswer;  

alert("correctAnswer="+cAns);

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 ,
Mar 30, 2016 Mar 30, 2016

Awesome! that worked. Thank you so much. Really appreciate it.

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 ,
Feb 22, 2018 Feb 22, 2018

How did you link the external .js file to the index? Where exactly? And where did you put the .js file?

I would like to recall the function written in my .js file in my Scorm through an hyperlink that execute a simple javascript code: the code should assign the variable, recall the function (in the .js file) and make it work with the assigned variable. Could you help me?

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 ,
Feb 26, 2018 Feb 26, 2018
LATEST

You'll need to edit the html template file located here:

C:\Program Files\Adobe\Adobe Captivate ## x64\HTML\index.html

Add the following include to the head section:

<script src="assets/js/yourfile.js"></script>

Then drop your JavaScript file in the folder:

C:\Program Files\Adobe\Adobe Captivate ## x64\HTMLassets\js\

If the folder does not exist, create it.

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