Forum Discussion
Referencing Json files in Storyline 360
Storyline gives advice on how to add external Javascript to a course:
https://access.articulate.com/support/article/Articulate-Storyline-360-JavaScript-Best-Practices-and-Examples
By adding the following to the story.html file:
<script LANGUAGE="JavaScript1.2" SRC="/story_content/MyJavaScriptFunctions.js" TYPE="text/javascript"></script>
And then inserting the .js file into the \story_content folder.
Is there a way to insert a .json file into \story_content and have javascript reference it?
Hi Blake_RT here's another method for you. Insert the following JavaScript into your Slide Master.
function loadScript() { const scriptId = 'dynamicScript'; // Unique ID for the script if (!document.getElementById(scriptId)) { const script = document.createElement('script'); script.id = scriptId; script.src = 'story_content/MyJavaScriptFunctions.js'; // Replace with the actual script URL script.onload = () => console.log('Script loaded successfully.'); script.onerror = () => console.error('Failed to load the script.'); document.body.appendChild(script); } else { console.log('Script is already added.'); } }
Then just drop your JS file into your project.
I like to use a combination of WebObject and the script above, as using WebObject ensures your script is always bundled with your project (no need to remember to add it post-publish).
So, create a folder with an "index.html" file, then drop your JS file into the folder, so you might have something like this:
JSAssets
- index.html
- MyJavaScriptFunctions.js
Add a WebObject to your Storyline file (I add to a layer in the Slide Master), and browse to the folder containing your files, in my example JSAssets. The files index.html and MyJavaScriptFunctions.js are now bundled into your *.story file.
I then do a publish so I can obtain the unique WebObject ID located in story_content/WebObjects/{uniqueid}
I then use script in Storyline to load the JS file I've bundled. This is where you need the unique WebObject id.function loadScript() { const scriptId = 'dynamicScript'; // Unique ID for the script const WebObjectID= '6fXBddbYPlO '; // Unique ID for the Storyline WebObject if (!document.getElementById(scriptId)) { const script = document.createElement('script'); script.id = scriptId; script.src = `story_content/WebObjects/${WebObjectID}/MyJavaScriptFunctions.js`; // Replace with the actual script URL script.onload = () => console.log('Script loaded successfully.'); script.onerror = () => console.error('Failed to load the script.'); document.body.appendChild(script); } else { console.log('Script is already added.'); } }
You could load in a JSON file using the same method.
- SamHillSuper Hero
Hi Blake_RT here's another method for you. Insert the following JavaScript into your Slide Master.
function loadScript() { const scriptId = 'dynamicScript'; // Unique ID for the script if (!document.getElementById(scriptId)) { const script = document.createElement('script'); script.id = scriptId; script.src = 'story_content/MyJavaScriptFunctions.js'; // Replace with the actual script URL script.onload = () => console.log('Script loaded successfully.'); script.onerror = () => console.error('Failed to load the script.'); document.body.appendChild(script); } else { console.log('Script is already added.'); } }
Then just drop your JS file into your project.
I like to use a combination of WebObject and the script above, as using WebObject ensures your script is always bundled with your project (no need to remember to add it post-publish).
So, create a folder with an "index.html" file, then drop your JS file into the folder, so you might have something like this:
JSAssets
- index.html
- MyJavaScriptFunctions.js
Add a WebObject to your Storyline file (I add to a layer in the Slide Master), and browse to the folder containing your files, in my example JSAssets. The files index.html and MyJavaScriptFunctions.js are now bundled into your *.story file.
I then do a publish so I can obtain the unique WebObject ID located in story_content/WebObjects/{uniqueid}
I then use script in Storyline to load the JS file I've bundled. This is where you need the unique WebObject id.function loadScript() { const scriptId = 'dynamicScript'; // Unique ID for the script const WebObjectID= '6fXBddbYPlO '; // Unique ID for the Storyline WebObject if (!document.getElementById(scriptId)) { const script = document.createElement('script'); script.id = scriptId; script.src = `story_content/WebObjects/${WebObjectID}/MyJavaScriptFunctions.js`; // Replace with the actual script URL script.onload = () => console.log('Script loaded successfully.'); script.onerror = () => console.error('Failed to load the script.'); document.body.appendChild(script); } else { console.log('Script is already added.'); } }
You could load in a JSON file using the same method.
- MathNotermans-9Community Member
And the same solution you can use for global variables and functions... Every variable and every function in your dynamicscript/MyJavascript.js is available on every slide or trigger.
- MathNotermans-9Community Member
As WebObject... or into the resource folder and call the appropriate path.