Storyline 3: JavaScript Best Practices and Examples
Article Last Updated
This article applies to:
Use JavaScript triggers to create advanced interactivity in Storyline 3 courses. (To learn more about triggers, see this user guide.)
Best Practices
Although we don't provide support for JavaScript coding, these tips will help:
- Use the
player.GetVar
method to retrieve the value of Storyline variables, and use theplayer.SetVar
method to set the value of a Storyline variable. In other words, you can pull information from Storyline variables withplayer.GetVar
and push information into Storyline variables withplayer.SetVar
. - Don't include
<script type="text/javascript">
in your code. - Each JavaScript trigger can have up to 32,767 characters of code.
- You can't call JavaScript functions from one trigger in another trigger. However, you can call JavaScript functions within the same trigger.
- If you'd like to include all your JavaScript functions in a separate JavaScript (.js) file, place your custom JavaScript file in the story_content folder of your published output, then add the following line of code to the story.html file before the closing </head> tag.
<script LANGUAGE="JavaScript1.2" SRC="story_content/MyJavaScriptFunctions.js" TYPE="text/javascript"></script>
Call the appropriate functions in your Storyline triggers. Note that this method isn't officially supported by Articulate. - If you use jQuery in your JavaScript triggers, be sure to reference the jQuery library.
- Storyline doesn't have documented system variables that you can use in JavaScript. You may discover some by working with Storyline's published output or searching the community forums. Just be aware that they could interfere with course playback, and they might not work in all versions of Storyline.
- For general information on JavaScript coding, see w3schools.com or Codecademy.
Examples
Find examples of common JavaScript triggers below. And be sure to check out our JavaScript challenge and recap for dozens of demos from our wonderful community of e-learning developers!
Change the value of a Storyline variable
|
Popup message (text only)
|
Popup message (text and value of a Storyline variable)
|
Print current slide (HTML5 only)
The simple code above prints the current slide and the Storyline player. If you want to print the slide content only, not the Storyline player, see this JavaScript code from Brian Batt. |
Print completion certificate See this demo and downloadable example by Tracy Carroll where she uses two JavaScript triggers to get the current date, capture the learner's name, and print a certificate. |
Launch new email message
|
Auto-scroll web page to specific location
|
Auto-scroll web page relative to current location
|
Change HTML background color
|
Change HTML background image
|
Generate a random number
|