Skip to main content

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

The problem

I have Storyline JavaScript snippets. Will they work here?

Step by step

  1. Make the variables the script uses

    Variables → Add Variable: LearnerName (text) and Greeting (text). Names must match the script exactly, capitals included.

  2. Add the action

    On a button, add a trigger, then Then → type "script" → Run JavaScript.

  3. Paste the snippet

    Paste your Storyline code into JavaScript, or pick Greet the learner by name from Insert an example….

  4. Click Try it

    Try it runs the script once and lists what it set, or the line that broke.

  5. 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

  1. Make the variables your script reads and writes: click Variables → Add Variable. For the example, LearnerName (text, starting value "Sam") and Greeting (text, empty).
  2. Select a button. In Triggers, click Add Trigger and open it.
  3. Under Then, click the action, type "script" and pick Run JavaScript. Storyline calls it Execute JavaScript.
  4. 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") and SetVar("Name", value), and Storyline 360's newer getVar and setVar. 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("…"), addToTimeline and other Storyline object APIs. Use triggers: Show object, Change object state, Move along motion path.
  • document.getElementById on slide objects, or reaching into window.parent. Set a variable instead and let a trigger react to it.
  • alert() and prompt() 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.