Javascript in Storyline: 2 Examples
Nov 29, 2017
Howdy all,
First time posting, but I just thought I'd share two snippets of code I'd been working on.
1. Keyword Search:
I wanted to be able to search a user input field for certain words and, based on whether the words were hit or not, adjust a variable in SL. It's currently used to ask users about their course goals and then highlight any that match what we might be covering in the course.
You'll want to set some of the variables beforehand in SL.
function keywordSearch () {
var player = GetPlayer(),
userGoals = player.GetVar("userGoals").toUpperCase();
array1 = ['FUNDRAISING', 'RAISING MONEY', 'DEVELOPMENT'],
array2 = ['EATING CAKE', 'EATING PIE', 'EATING COBBLER'],
array3 = ['WAILING', 'SOBBING', 'CRYING'];
for (var i = 0; i < array1.length; i++) {
if (userGoals.includes(array1[i])) {
player.SetVar('goal1', "Fundraising");
}
}
for (var i = 0; i < array2.length; i++) {
if (userGoals.includes(array2[i])) {
player.SetVar('goal2', "Consuming baked goods");
}
}
for (var i = 0; i < array3.length; i++) {
if (userGoals.includes(array3[i])) {
player.SetVar('goal3', "Feeling sorry about your life");
}
}
}
keywordSearch();
2. Another thing I made was a word counter (to limit the number of words within a text entry field). So long as you have the counter adjust a variable, then you can use that to trigger other sorts of actions (a warning message, change of state, etc.)
P.S. I cheated... because counting spaces is much easier than actually counting words.
var player = GetPlayer(),
spaceCount = 0;
for (var i = 0, x = player.GetVar("userGoals"); i < x.length; i++) {
if (x[i] === ' ') {
spaceCount++;
}
}
if (spaceCount > 19) {
Number(player.SetVar("userGoalError", 1));
Number(player.SetVar("numWords", (spaceCount - 19)));
} else {
Number(player.SetVar("userGoalError", 0));
Number(player.SetVar("numWords", 0))
}
12 Replies
This is great, Kody! Thanks for sharing.
Do you have an example .story file with this in it that you can share?
Agreed! Any chance you can share a .story file, Kody?
Hi Lori,
I'm not sure if Kody is still subscribed here since there was no reply from Brian's inquiry.
You are certainly welcome to reach out to the user directly via the 'Contact Me' option on the user profile if you do not hear back soon.
This is NOT Kody's file. However, I created this following his instructions. Use the keywords and under 10 words to see what happens. Then increase your word count to more than 10 and try again. This will show you both sets of code in action that Kody created.
See it in action (SL360) here.
SL2 file attached below.
Owen you are a wonderful human being! :-) Thank you!
Thanks so much Owen! I can see you made Lori's day :)
My pleasure. I hadn't applied any JavaScript like this before so I was curious myself to see it in action and understand what Kody had coded.
The challenge we're facing is that "includes" isn't a javascript statement that works in Internet Explorer. Apparently you can use "indexOf" but we haven't worked out exactly how yet... anyone up for trying it out?
From StackExchange: "String.prototype.includes is, as you write, not supported in Internet Explorer (or Opera)
Instead you can use String.prototype.indexOf. #indexOf returns the index of the first character of the substring if it is in the string, otherwise it returns -1. (Much like the Array equivalent)"
So simply put it before the other function statement that has the "includes" statement?
Can you tell I only know enough to be dangerous with Javascript? :-)
I've tried copying and pasting this prior to the code in the "execute javascript" trigger and it still works fine in Chrome and still doesn't work in IE...
Ran it with a trigger set to timeline starts. File worked in Chrome. Opened but wouldn't run code in I.E. Player loaded but not the slide in Edge.
If I had any sense at all I would have just checked out Matthew's solution from his blog first! Duh... elegant without the use of "includes" - that's time I won't be wasting in the future - check Matthew's stuff first! Works perfectly...
This discussion is closed. You can start a new discussion or contact Articulate Support.