Javascript not working in Storyline
Jan 14, 2016
Hi,
I am trying to use a Javascript code in Storyline that will change a variable over time, and will continue to change it over time. I use this script in a sample page and it worked. Now I am trying to use the same script in Storyline but it does not work. Is there an issue with my syntax? How can I accomplish this?
This is the original Javascript
<div id="Annie"></div>
</script>
var customer = 0;
window.setInterval(
function () {
customer = customer + 1;
document.getElementById("Annie").innerHTML = "You have " + customer + "(s)in your store";
}, 1000);
</script>
This is my Storyline interpratation
var player = GetPlayer();
//var customers = player.GetVar("Annie");
var customer = 0;
window.setInterval(
function () {
customer = customer + 1;
var customers = "You have " + customer + "(s)in your store"";
}, 1000);
player.SetVar("Annie", customers);
Thank you guys!
12 Replies
Hi, Gustavo -- Thanks for reaching out with your question! In case you weren't aware, I wanted to note that we are not able to provide support for JS coding, but there are lot of JS gurus here in the community who may be able to stop in to assist. Please also feel free to take a look at this sheet on JS Best Practices, as well. :)
Try removing the "//" from "//var customers = player.GetVar("Annie");"
Also looks like you may have an additional "
Hi guys,
Christie thank you for your reply. Yes I am aware that the forum is not to provide support for JS coding. My question is due to the fact that I have not been able to get storyline work with the window.setInterval() JS method.
Michael I commented that line out for the example.
Phil I removed the extra " but it still does not work.
I guess at the end I am trying to find a solution where I can get the method window.setInterval to work in storyline so I can change a variable over time.
Thank you for all the replies.
I always add a JavaScript alert on the first slide to see if the syntax is correct. As storyline loads all code at once and any error will
Stop all the code.
I haven't tried the code just gave it a cursory glance.
I will see if I can find some time tomorrow to run it.
Sent from my iPhone
Hi everyone,
I was able to figure out how to get the JavaScript to work. Here is an example of a running clock.
same thing with me i.e. javascript stopped working, coould be becuase multiple javascript libraries are loaded... see page source. I've found no solution yet
Hi Chris and Gustavo,
Any time there is an error in any javascript code, javascript will stop executing at the line with the error, and will ignore any remaining code. Often, there is nothing to show what failed, or why.
Here are some hints about how you can determine what is happening with javascript fails.
A good way to check your code, is to use your browser's built in development mode. This should work with any browser.
When you open your browser, or when a new browser window opens to run a course. press the F12 button. This will open the developer's tools for the browser. Select the console tab to view the console output. Javascript always prints any errors to the console be default, and you can see any errors that javascript reports in this console tab.
Now that you can see messages from javascript, you can include messages in your code to let you know where things happen in the code. For example, if I add the line:
console.log("Hello World!");
javascript will send the text Hello World! to the console, and you can see that message in the console window.
If my javascript code isn't running, I will often include comments like this to determine where my code failed:
console.log("Line 1 just ran");
If I add that line between each line in my code, and update the line number, now I can see exactly where javascript stopped running, and I know which line of code I need to fix.
You can also use this to return the value of any variable in your code. For example, let's say I have a variable x, and I want to see what the current value of that variable is.
console.log("The value of x = ", x);
Assuming the value of x is 20, the previous line will print to the console this:
The value of x = 20;
I hope that this information helps those of you that want to use javascript to enhance your Storyline modules.
Hi Dave,
I found this quite helpful. I'm having some trouble with my own script.
My problem is that when I open the console window and then launch my course. I get this error on the first page (on which there is no javascript):
Uncaught SyntaxError: Invalid or unexpected token
I get nothing after that. Any ideas?
Thanks, Jacqueline
Hi Jacqueline,
If this error is on the first page, and you haven't added any additional JavaScript, then you can probably ignore it.
Sometimes you may get errors from the JavaScript that is built into Storyline. There is so much JavaScript in there that is intended for various environments that you can sometimes see an error reported in the console.
Any script that you add should attempt to run on it's own. You can test it by running the hello world script listed above.
Once get that error, I get nothing else. I've tried manually retyping the code and line by line, I publish and retest. That is working, but slowly. I'm thinking it has to do with my company's browser protection.
Thank you for the feedback.
What line of code are you typing? I thought you said you didn't have any script yet?
If you are trying to use the line of code from above, it appears I typed it wrong. Try this instead.
console.log("The value of x = " + x);
This discussion is closed. You can start a new discussion or contact Articulate Support.