Skip to main content
Participant
Sep 26, 2018 1:52 PM UTC
Question

Javascript scope in two different script windows.

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 is closed to new replies. Start a new post to keep the conversation going.

6 replies

MeteonAuthor
Participant
Sep 27, 2018 9:34 AM UTC

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.

Translate
Inspiring
Sep 26, 2018 3:49 PM UTC

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.

Translate
TLCMediaDesign
Inspiring
Sep 26, 2018 4:55 PM UTC

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

Translate
Inspiring
Sep 26, 2018 6:48 PM UTC

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

Translate
TLCMediaDesign
Inspiring
Sep 26, 2018 1:57 PM UTC

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.

Translate