Loading an external javascript library
May 07, 2015
Perhaps someone finds this useful.
I'm testing a simple mechanism to load a external Javascript file. To do so I add a "execute javascript" trigger to the slide on timeline start. I add the following code to this trigger:
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.src = 'story_content/library.js';
script.type = 'text/javascript';
head.appendChild(script)
In this example I've placed the javascript file in the story_content folder, but you could store it on another webserver as well.
I've used this mechanism to create password strength checker functionality for Storyline.
7 Replies
Very cool Niels! I will definitely be trying this out soon :)
Hi Niels -
I use a similar setup. You'll want to make sure that it only loads once. Checking for the existence of the element and only loading it if it's not been created would do the trick. Otherwise, every time the trigger is run it'll add the new script element to your page.
I'll usually create a function for this to load multiple script files through the same interface. It's a little overkill as I'm not saving much but it was fun:)
I add the JS files as "carry on" as a web object. This loads the files into the project so you don't have to do any post-publish surgery.
Here's an example that loads in the SoundManager library to loop background music:
https://community.articulate.com/discussions/building-better-courses/looping-one-single-audio-track-throughout-all-slides#reply-31463
Thanks Steve for the additions... I was thinking on creating a mechanism to unload code after use, so the code would only be available on the slide. Have to find an easy and maintainable way to do that.
Hi, Matthew -- In case you don't hear back from Steve and he is no longer subscribed to this thread, you are welcome to reach out directly via his profile page. :)
Sounds great, Matthew. :)
Hi all
A bit late to the party but I'm currently looking to use externally hosted Javascript within a project.
I noted that Niels is using as a password strength check - would this method allow passing of a variable to an external JS file, please?
Specifically, the students name as a variable from this? (https://community.articulate.com/discussions/articulate-storyline/retrieve-lms-user-name-as-variable)
Many thanks :)
Hi Aaron! This thread is a bit older so I'm not sure if everyone is still subscribed. You can also reach out to Niels directly via the 'Contact Me' feature in his profile.
This discussion is closed. You can start a new discussion or contact Articulate Support.