Run JavaScript: paste your Storyline GetPlayer() snippets
Paste a Storyline Execute JavaScript snippet into a Run JavaScript action, check it with Try it, and use what it sets in the next action. What works (GetVar, SetVar, getVar, setVar, dates, maths, requests in a package) and what doesn't (the slide's page, object(), alert).
Last updated 6 min read
I have Storyline JavaScript snippets. Will they work here?
Step by step
Make the variables the script uses
Variables → Add Variable:
LearnerName(text) andGreeting(text). Names must match the script exactly, capitals included.Add the action
On a button, add a trigger, then Then → type "script" → Run JavaScript.
Paste the snippet
Paste your Storyline code into JavaScript, or pick Greet the learner by name from Insert an example….
Click Try it
Try it runs the script once and lists what it set, or the line that broke.
Show the result
Add a text box with
%Greeting%, then Preview and click the button.
What you'll build
A button that runs a Storyline snippet and greets the learner by name. Along the way you'll see the Try it button, which tells you whether a script works before you preview.
Add a Run JavaScript action
- Make the variables your script reads and writes: click Variables → Add Variable. For the example,
LearnerName(text, starting value "Sam") andGreeting(text, empty). - Select a button. In Triggers, click Add Trigger and open it.
- Under Then, click the action, type "script" and pick Run JavaScript. Storyline calls it Execute JavaScript.
- Paste your code into the JavaScript box, or choose an example from Insert an example…:
var player = GetPlayer(); var name = player.GetVar("LearnerName"); player.SetVar("Greeting", "Welcome, " + name + "!");
Try it
Click Try it under the code. It runs the script once, safely, with your variables at their starting values, and tells you one of three things:
- What it set, for example
Greeting = "Welcome, Sam!". It works. - An error and its line number. Fix that line and try again.
- That it is still running after 3 seconds. Usually a timer or a web request. It keeps running in the course.
Then add a Text box with %Greeting%, click Preview and click the button. The greeting appears.
Tip: Preflight warns you when a script uses a variable the course doesn't have. GetVar would get nothing and SetVar would do nothing, so check the spelling and the capitals.
What works
GetPlayer().GetVar("Name")andSetVar("Name", value), and Storyline 360's newergetVarandsetVar. Every variable in the course, by name.- Plain JavaScript: maths, dates, text, arrays,
Math.random(). - Waiting for the script. The trigger stops at a Run JavaScript action until the script finishes (up to 1.5 seconds), as in Storyline. So the next action, or a condition, already sees what the script set.
- Changes that land later. A timer or a web request that calls SetVar afterwards still updates the variable, and When variable changes fires.
- In an exported package, scripts may load a library over https and send requests, for example an xAPI statement to your LRS. The preview in Scormbusters blocks other sites, so test those in your LMS.
What doesn't, and what to do instead
The script runs in a sealed frame of its own, for safety. It cannot reach the slide's page. So these Storyline habits won't work:
object("…"),addToTimelineand other Storyline object APIs. Use triggers: Show object, Change object state, Move along motion path.document.getElementByIdon slide objects, or reaching intowindow.parent. Set a variable instead and let a trigger react to it.alert()andprompt()are blocked. Show a layer instead.- Saving to
localStorage. Use a variable: variables are saved with the learner's progress.
When you import a Storyline course, scripts that only read and set variables come across as Run JavaScript actions. The rest are counted in the import report so you know what to rebuild.
Do you need JavaScript at all?
Many Storyline snippets exist only because Storyline can't do the maths. Here, Adjust variable handles multiply, divide, remainder, random numbers and formulas like Score * 2 + Bonus, and %Name% shows a variable in text. See Variables, operators and formulas. Reach for Run JavaScript for dates, text handling and talking to other systems.
Part of the documentation: Build.
Found something here that does not match what the product does? That is a bug in the guide — see all guides or tell us from the Help Center.