Calling Javascript more than once

Feb 27, 2017

I have an activity that I am trying to create where a user enters three values and then I use Javascript to concatenate those values as an URL. I want the user to be able to experiment with different values so I was hoping that by clicking the same button the new values would update the URL.

Unfortunately, the initial values remain each time. I tried using another button to clear the values using javascript but for some reason the original script ceased to work.

I am not a programmer and I was feeling quite elated getting as far as I did.

Here is the code I am using to generate the URL:

var player=GetPlayer();

var _AccountNum=player.GetVar("AccountNum");

var _RoutingNum=player.GetVar("RoutingNum");

var _AmountNum=player.GetVar("AmountNum");

player.SetVar("Account_Routing","transfer?fromAcct=1&fromTrans="+_AmountNum+"&toAcct="+_AccountNum+"&toRouting="+_RoutingNum);

The "Transfer" (attachment) button displays the result as well as a highlight box around the URL. The "Clear" button hides the result and the highlight box.

I even tried getting the Clear button to Jump To the current slide in the hope of resetting everything.

Any help appreciated.

8 Replies
Dave Cox

Hi Teg,

I'm looking at your javascript, and I must admit, I'm not really sure what you are trying to do.

The code that you have on the Transfer button appears to work just fine. I don't see any javascript on the clear button.

Javascript can be quite daunting to use and troubleshoot, but here are a few hints and tricks to help you with your javascript journy.

Remember, javascript will only run in the published project, not in the development environment, so you must publish and run to test your code.

The windows console is your friend. You can get to the windows console in any browser by pressing F12 when your project starts.

console.log is your next best friend. You can use console.log to view what is going on with your code. While testing, use it everywhere! 

I modified your code as following so I could make sure everything is running:

console.log("js"); // make sure js actually starts.
var player=GetPlayer();

var _AccountNum=player.GetVar("AccountNum");
console.log("AccountNum = " + _AccountNum); // I got the AccountNum
var _RoutingNum=player.GetVar("RoutingNum");
console.log("RoutingNum = " + _RoutingNum); // I got the RoutingNum
var _AmountNum=player.GetVar("AmountNum");
console.log("AmountNum = " + _AmountNum); // I got the AmountNum
player.SetVar("Account_Routing","transfer?fromAcct=1&fromTrans="+_AmountNum+"&toAcct="+_AccountNum+"&toRouting="+_RoutingNum);
console.log("Result = " + _AmountNum+"&toAcct="+_AccountNum+"&toRouting="+_RoutingNum); // I sent this back to Account_Routing

Notice all of the nifty little console.log statements. 

Anytime you see "//" everything after that is a comment. It give you a way to comment your code so you can see what you are doing.

The console displays, (I entered 1, 2 and 3 in the fields):

js
AccountNum = 1
RoutingNum = 3
AmountNum = 2
Result = 2&toAcct= 1&toRouting= 3

Javascript will stop running immediately when any line of code fails to run, and the remaining lines will not run. For this reason, I will often place a console.log statement after every line of code so I can see just where the code quit.

The other thing to do when something doesn't work as expected, either with javascript, or just with your triggers in Storyline, simplify. You have a lot going on in the project. Try implementing your features one at a time, and test each item as you implement it. Whenever possible, try doing everything you can without javascript. It will make your life must easier. When you must use javascript, Put default and test values in your code to test your triggers before you implement your javascript. Always activate javascript last, only after you know everything else works. Storyline doesn't really give you much for text manipulation, so your are probably on the correct path for this. But for testing, you can assign a text variable with the value of another text variable.

I hope this information helps. I'll be happy to help you with specific questions that you can't figure out.

I modified your project some as part of my testing, so I'm sending it back to you so you can see what I did. I only modified that one slide that you referenced.

TegOldAccount Griffiths

Many thanks Dave,

I inherited this course and the activity was built in Flash. Part of my remit is to remove all flash related content. Until this, I have not had a problem. The whole idea is that a student can enter the values in the three fields and when they click submit, the information is displayed in the URL (not good security/coding practice) and this shows a vulnerability in the code. This should be repeatable with different values.

I tried adding javascript to run off the Clear button but that then prevented the script from the submit button from working.

I also added triggers to clear the data from the fields and when you clicked the Clear button it reloaded the slide. This cleared the values in Storyline but not from the Javascript and resubmitting the values did nothing.

I am as far away from being a coder as a person can get, so this is more challenging than usual for me.

Dave Cox

Hi Teg,

There isn't any reason why you can't run javascript on more than one trigger. I do it all of the time with no problem. 

Still, I would only use javascript when necessary to do what you need to do. You can clear all of the values of your variable with just triggers. I've now added the triggers to do that for you.

You don't need to worry about clearing the javascript variables, as those are re-initialized each time you call them.

You also don't need to hide the text box that displays the Account_Routing number. I think that bit was causing your issue. I removed all of the triggers, and set the text box to always display. It won't display anything when it is blank, so those triggers aren't needed. I also used triggers to clear everything when the clear button is clicked. 

I think your code will work as expected now. Please let me know if you have any additional questions. I understand triggers and javascript can get confusing at times.

This discussion is closed. You can start a new discussion or contact Articulate Support.