Highlighted

Javascript scope in two different script windows.

New Here ,
Sep 26, 2018

Copy link to clipboard

Copied

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.

Views

96

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Javascript scope in two different script windows.

New Here ,
Sep 26, 2018

Copy link to clipboard

Copied

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.

Views

97

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Sep 26, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Engaged ,
Sep 26, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Sep 26, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Engaged ,
Sep 26, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Sep 26, 2018

Copy link to clipboard

Copied

Got it, I thought it was his array since he asked about globals, need to parse the Germ? better.

It is bad form to have functions inside of functions or methods anyway.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Meteon LATEST
New Here ,
Sep 27, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Resources
Captivate User Guide
New Group