Javascript not activating in Storyline-(showing only default value)

Jun 03, 2021

Hello,

I am trying to get a basic date to generate using JavaScript. I have everything set up and when published it only shows the default value. Please help. 

Here is the code I am using: 

<script>var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var dateString=month + "/" + day + "/" + year
var player = GetPlayer();
player.SetVar("TodayIs",dateString);
player.SetVar("month",month);
player.SetVar("day",day);
player.SetVar("year",year);
</script>

4 Replies
Russell Killips

Your script is missing semicolons.

var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var dateString=month + "/" + day + "/" + year;
var player = GetPlayer();
player.SetVar("TodayIs",dateString);
player.SetVar("month",month);
player.SetVar("day",day);
player.SetVar("year",year);

 

Joseph Francis

I was going to offer Storyline 360: JavaScript Best Practices and Examples as having some good information. Then I looked closer at their "Change the value of a Storyline variable" example, and lo and behold the first 5 JavaScript statements are not terminated with semicolons! Seriously, Articulate? This conversation has been around longer than you have.

Semicolon Insertion: July 2000 Draft, JavaScript 2.0, Rationale

Do you recommend using semicolons after every statement in JavaScript?

I don't trust a parser to automatically insert a semicolon where it thinks it might need to go. That's why programming languages like C, C++, and Java specify using a semicolon to let the compiler know it has reached the end of a command.

If you want to test your JavaScript, copy and paste it in JSLint. Unsurprisingly, it did not like what came from Articulate, but it did quite a bit better with Russell's version (JSLInt obviously wouldn't understand player.GetVar / player.SetVar).