Javascript Causing Slide Not Work

I am new to Javascript and am trying to learn.  I have a slide that worked great until I tried to execute Javascript.  Now the slide doesn't work as it did before.  I am attaching the file.

The user drops the currency in the drop zone and it is supposed to total below as each image is dropped.  I added Javascript so that the dollar amount would have two fixed decimal places.  Now, nothing shows for the total.  Any help would be greatly appreciated!

12 Replies
Dave Cox

Hi Tammy,

Using variable names that are the same as your articulate variable isn't a problem. The variable scope is different, and the don't interact.  

Looking at your script, the problem appears to be that your javascript only runs when the timeline starts, not when the interaction takes place. You would need to create a trigger that would cause your javascript to run every time the value to Total changes for this to work. I've made the change to your file for you, and now it is working.

Keep in mind, that javascript only runs in the published project, so it still won't display a total when you run it in the development mode.

Tammy Wise

Thank you for the feedback!  The slide works as it should now except that the Javascript is not produces the desired results.  I added it so that I could have two decimal places in the currency total.  It is not consistently doing this.  

I am not sure what the difference is between Storyline variables and Javascript variables.  Can anyone explain this?

Dave Cox

Looking at your javascript, only declare each variable with the var statement once. By using the var statement with a variable that is already defined can prevent the javascript from running correctly.

var player=GetPlayer();
var Total=player.GetVar("Total");
Total=Total.toFixed(2);
var TextTotal=Total.toString();
player.SetVar("TextTotal", TextTotal);

Dave Cox

Hi Tammy,

That is actually a very good question! 

It took me awhile to figure it out, but the answer is this: The script is working. The problem isn't with the script at all. The problem lies with the Storyline code,  and how it is parsing the value placed back into Storyline's TextTotal variable. It appears that in HTML5, Storyline is treating the value like a number, and stripping the zeros after the decimal point. (Probably some additional javascript is the Storyline HTML5 player.) The only good work around that I've been able to make work is to add an additional character, like a period. A space doesn't seem to work.

So the new code would look like this:

var player=GetPlayer();
var Total=player.GetVar("Total");
Total=Total.toFixed(2);
var TextTotal=Total.toString();
TextTotal = TextTotal + '.';
player.SetVar("TextTotal", TextTotal);

Notice the additional line that I added to add a period at the end.

Perhaps, if we are lucky, one of the Nice people at Articulate could look at this behavior in Storyline to determine why this is? Maybe even fix it?