February 1, 2021

Tutorial: JavaScript + Storyline to create a day-of-the-week greeting

Easy JavaScript + Storyline code snippet

I created this snippet of JavaScript for Storyline while working through Zsolt Olah’s POP(99) tutorials (which I cannot recommend highly enough). This code snippet is mostly taken from this Stack Overflow discussion and I have lightly spruced it up for our Storyline purposes.

This code can be executed when the timeline starts on a Storyline slide. Just place the snippet below into an "Execute JavaScript" trigger in Storyline. The JavaScript sets a Storyline variable (in this example, “greeting”) with a greeting that includes the current day of the week.

Here is the snippet:

{% c-block language="js" %}
var player = GetPlayer();
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var d = new Date();
var dayName = days[d.getDay()];
var Message = 'Happy ' + dayName + '!';
player.SetVar("greeting", Message);
{% c-block-end %} 
This code assumes that there is a variable named “greeting” already created in the Storyline file.

Now, in a Storyline textbox, the text “%greeting%” will be replaced (once the course is published) by the full string created in our JavaScript code.

Have fun with it!