Forum Discussion
Build Custom Search within Storyline using JavaScript
To explain what is happening in the JavaScript code, see explanation below:
var player = GetPlayer();
This sets up the communication between JavaScript and Storyline and allows JavaScript to see and set Storyline Variable Values.
var parameters = player.GetVar("searchfield");
This declares a JavaScript variable called "parameters" (you could name this something else as long as you pull the name into the next line of code) and its value equal to the Storyline Variable "searchfield" value (aka whatever the user types into the Search bar). So if the user types in "cat" the JavaScript variable "parameters" is now equal to cat.
var lower = parameters.toLowerCase();
This declares a JavaScript variable called "lower" (again, you could name something else if desired as long as you carry it through the code wherever lower is) and its value is equal to the variable value of "parameters" but in lower case. So, this converts whatever the user has typed into the search bar into lowercase and that is the value of the "lower" JavaScript variable. So if the user typed in Cat, it is now cat and the value of the "lower" variable.
var key = "(insert your slide's keywords here inside the quotes without the parenthesis)";
This declares a JavaScript variable called "key" and you can make its value whatever you want the search terms to be, within the " ". Ex. "cat dog horse" Ensure you put these in lowercase since we're only working with lowercase letters.
var termlocation = key.search(lower);
This declares a JavaScript variable called "termlocation" which is equal to the position of the user's search entry within the overall search terms. Ex. If the search terms are "cat dog horse" and the user searches for cat, it will return a value of 0, dog a value of 4, and horse a value of 8.
player.SetVar("placement",termlocation);
The final step is to set the Storyline variable "placement" value = the JavaScript variable "termlocation" value. This way we can use the number determined in the last step within Storyline. Without this, the programming will not work.
Then, in your course the user enters a search term, clicks search, and the placement variable shows a value. Use this value to make triggers happen - ex. jump to a slide when placement = #, Show a layer when placement = # , Change a State of an Object when placement = #.