5 JavaScript techniques every Storyline 360 developer should know
Storyline’s triggers and variables can do a lot. But at some point every developer hits a wall: a calculation that’s too fiddly, text that needs formatting, or an interaction Storyline simply doesn’t offer.
That’s where the Execute JavaScript trigger comes in. You don’t need to be a programmer to use it well. These five techniques cover most of what we use day to day.
Before you start: JavaScript runs in the published course in a web browser. Always test in the published output (for example in Review 360 or on your LMS), not only in Preview.
1. Read and write Storyline variables
Almost everything starts here. JavaScript can read a Storyline variable, do something with it, and write a result back.
// Works in every version of Storyline 360
var player = GetPlayer();
var score = player.GetVar("Score");
player.SetVar("ScoreMessage", "You scored " + score + " out of 10");
Newer versions of Storyline 360 also include shorter helper functions:
var score = getVar("Score");
setVar("ScoreMessage", "You scored " + score + " out of 10");
Variable names must match exactly, including capital letters. Most “my JavaScript doesn’t work” problems come down to a typo in a variable name.
2. Format dates and numbers
Storyline can’t format a date on its own, but JavaScript can. This is useful for certificates, personalised messages and scenario realism.
var today = new Date();
var text = today.toLocaleDateString("en-US", {
year: "numeric",
month: "long",
day: "numeric"
});
setVar("TodayText", text); // e.g. "November 23, 2026"
The same approach formats money:
var amount = getVar("OrderTotal");
setVar("OrderTotalText", amount.toLocaleString("en-US", { style: "currency", currency: "USD" }));
Put %TodayText% or %OrderTotalText% in a text box and Storyline shows the formatted value.
3. Randomise practice values
Practice gets stale when the numbers never change. If learners repeat a calculation activity, give them new values each time.
function randomBetween(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var price = randomBetween(20, 90);
var discount = [10, 15, 20, 25][randomBetween(0, 3)];
setVar("Price", price);
setVar("Discount", discount);
setVar("CorrectAnswer", Math.round(price * (1 - discount / 100)));
Run this when the slide starts, then compare the learner’s answer with CorrectAnswer using a normal Storyline trigger.
4. Move and rotate objects
Recent versions of Storyline 360 let JavaScript work directly with objects on the slide using object(). You insert the object’s ID from the JavaScript editor, then change its properties.
var needle = object("6abcDEF1234"); // replace with your object's ID
var value = getVar("Confidence"); // 0 to 100
// Turn a 0–100 value into a gauge angle from -90 to 90 degrees
needle.rotation = -90 + (value / 100) * 180;
This is how we build gauges, meters and custom sliders that respond instantly to what the learner does, with no extra states or motion paths.
5. Call a web service safely
JavaScript can send data to, and get data from, a web service: saving a reflection, looking up content, or talking to an AI model.
fetch("https://your-proxy.example.com/feedback", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ answer: getVar("LearnerAnswer") })
})
.then(function (r) { return r.json(); })
.then(function (data) { setVar("FeedbackText", data.feedback); })
.catch(function () { setVar("FeedbackText", "Feedback isn't available right now."); });
One rule matters more than any other here: never put an API key or password in your JavaScript. Anyone can read a published course’s code in their browser. Send requests to a small server you control (a “proxy”), and keep keys there. Always include a fallback message in case the connection fails.
Keep your JavaScript maintainable
- Comment every trigger. A one-line comment saying what the code does will save the next developer, and future you.
- Keep logic in one place. If several slides use the same function, define it once on the first slide or the slide master instead of copying it everywhere.
- Fail gracefully. If something goes wrong, the learner should still be able to continue.
Once you’re comfortable with these five, you can build most of what Storyline doesn’t do out of the box.