Forum Discussion
Build Custom Search within Storyline using JavaScript
Using JavaScript, follow these instructions to add a Custom Search Engine to your course. In 2018, I shared this functionality via a 5 minute Spark Presentation at the Chicago eLearning & Technology Showcase in Naperville, IL.
Note: The instructions below were programmed using Articulate Storyline 2, and I have ensured they are applicable in Storyline 3 and Storyline 360 as well. These instructions are specific to jumping to a specific slide when a user enters specific search terms, however you could also have other actions occur when this happens, such as displaying a layer or changing an object's state to show applicable search results.
Build Custom Search Engine in Storyline:
1. Add a "search" slide (your slide 1.1) in Articulate Storyline.
2. On this slide, add a Text Entry Field and edit the variable associated with the entry field. Click the + sign within the variable popup to add a new variable.
3. Name the new variable: searchfield and make it a Text variable with an initial value of (blank).
4. Also on your search slide, add a button and add "Search" as the button's text.
5. Add a new variable: placement and make it a Number variable with an initial value of 0.
6. Add a reference to the placement variable on your slide by adding a text box and entering the text: %placement% into the text box.
7. Create your content slide(s).
Optional: On each content slide, add a button which contains a trigger to jump to slide 1.1 when the user clicks it so that the user can easily return to the search slide.
8. For each content slide, think about the keywords that the user might enter into a search bar in order to find that content. List out these keywords in a notepad and make them all be in lowercase letters. (JavaScript is case sensitive and this example only works with lowercase letters for ease of programming).
9. Return to the search slide and edit the search button's default trigger.
Action: Execute JavaScript
Script:
var player = GetPlayer();
var parameters = player.GetVar("searchfield");
var lower = parameters.toLowerCase();
var key = "(insert your slide's keywords here inside the quotes without the parenthesis)";
var termlocation = key.search(lower);
player.SetVar("placement",termlocation);
When: User Clicks
Object: Search Button
On Condition: searchfield != Not equal to (blank)
10. Publish your course to HTML to test it.
Note: You cannot preview JavaScript programming in Storyline, you have to publish it to test it.
11. Enter each of your keywords, one at a time, into the text entry search field and click the search button.
12. Note the value of the placement variable reference on the slide for each of your keywords.
Ex. keyword 1 = 0 keyword 2 = 10 keyword 3 = 20
13. On the search slide, add a trigger on the search button to:
Action: Jump to slide
Slide: (appropriate content slide corresponding to the first keyword(s))
When: User Clicks
Object: Search button
On Condition:
placement == Equal to (insert placement # corresponding to keyword)
Ex. Jump to slide 1 when placement == Equal to 0
14. Repeat step 13 as needed to add any additional triggers to jump to additional slides when the placement variable is equal to the correct corresponding keyword #.
Ex. Jump to slide 2 when placement == Equal to 10
15. Remove the placement variable reference (%placement%).
16. Publish to HTML.
Finished result: User enters a keyword into the text entry search field, clicks search button, and the corresponding slide displays.
Happy programming!
- DavidGlow-75b12Community Member
Will this search within text fields that contain variables?
For example, I have text input field that writes to "TextEntry" variable.
I put that %TextEntry% into a textbox on a slide.
Would the search find the results within that textbox stored in the TextEntry variable?
The standard search functionality in the player will not. TYIA for the insights!- BridgetODellCommunity Member
Hi David,
Thanks for reaching out with this question. Can you expand on your inquiry?
As I understand, from what you've written above, the user types an entry into a text entry field. Whatever they enter is assigned as the value of the TextEntry variable. You then have added a reference to that value on a slide.
Are you then asking if you add a second text entry field to act as the search entry, if that could also search for whatever term was entered into text entry field one?
- WesleyAleshireCommunity Member
Is there a way to add a function that directs the user to a "unknown term" slide when the user types in a word that isn't used in the search function?
- BridgetODellCommunity Member
@Content Creators - yes, you could do this.
When the user's text entry can't be found within the specified search terms, it will return a value of -1, which will be set as the value of the placement variable.
So, on your search button, you could add a trigger to "Jump to Slide" Unknown Term Slide when User Clicks if placement variable = -1.
Let me know if you have any additional questions on this.
- IvorRusselCommunity Member
Bridget, Would it be possible for you to share an example source file? I'm still facing some challenges with creating the custom search and I believe an example would be really helpful. Thank you for your assistance!
Hi Ivor!
Thanks for reaching out! Bridget may no longer be subscribed to this discussion, but you're welcome to contact them directly through their profile by clicking on their name and selecting Contact Me.
- IvorRusselCommunity Member
Hi Eric, Thanks, but I don't see Contact Me option when I click on Bridget's name.