Skip to main content
Participant
September 26, 2018
Question

Javascript scope in two different script windows.

  • September 26, 2018
  • 3 replies
  • 362 views

Hello,

I searched the helpf forums and I found that all javascript variables entered in captivate are supposed to be global.

But obviously I can't reference some variable in one script window which I defined in another script window.

I thought this should be possible.

I have a script for the "on enter slide" event which defines two functions:

try {

var alleSprecher =[];

function addSprecher(_element) {

  if(alleSprecher.indexOf(_element) >= 0) {

    alleSprecher[alleSprecher.length] = _element;

  }

}

function notifyAlleSprecher() {

  alleSprecher.forEach(sprecher => sprecher.classList.remove("anim");

}

}

catch(error) {

  alert(error);

}

And I have a button with an advanced action that does call a script besides doing other things. In this script I call these two functions:

try {

var buttonElement = document.getElementById("SmartShape_Sprecherin_rotc");

addSprecher(buttonElement);

notifyAlleSprecher();

if(!(buttonElement.classList.contains("anim"))) {

  buttonElement.classList.add("anim");

}

}

catch(error) {

  alert(error);

}

When pressing the button this script throws a ReferenceError stating "addSprecher" is not defined.

What am I doing wrong?

Is this even possible in Captivate?

Any help is very appreciated.

This topic has been closed for replies.

3 replies

MeteonAuthor
Participant
September 27, 2018

There is no block scope in Javascript except for the keyword "let". There is only global and function scope. So all variables and functions declared in the try block should be global.

Inspiring
September 26, 2018

Define the functions in index.html or in an external js file that is called by index.html -  they load at the beginning of the course session.  You can then call the functions from individual slides.

TLCMediaDesign
Inspiring
September 26, 2018

It won't matter if it's in external JS, the OP still needs to get the variable declaration outside of the try block.

Inspiring
September 26, 2018

Yes, but his error was for not finding his function declaration. Which was also in a try block.

TLCMediaDesign
Inspiring
September 26, 2018

They are not global if they are defined in a function or method. Try creating the variable in Captivate and then you can reference it.