Call function in web object from Storyline
Apr 05, 2018
Can someone assist with the syntax to call a function in a web object hosted in Storyline?
Within the web object I have a script that is called by a button: onclick="controls.reset();"
This works fine, but I want to call that function from a Storyline button by running JavaScript.
The issue I have is working out how to address the web object / iframe that the script resides in.
I have tried different variations of this type of code to no avail:
var x = document.getElementsByClassName("webobject");
x[0].HTMLIFrameElement.contentWindow.controls.reset();
Can anyone point me to a solution?
10 Replies
I'm betting for starters you are blocked by cross script domain issues so the course and web object are effectively running from two different domains. Modern browsers block these types of calls over security concerns.
Second, the course does not exist the DOM as you might expect. I would suggest you walk the DOM and see what markup Storyline generated; it might surprise you.
Hello Mark!
Your tries to access the content of the web object are very close. Since it is an iframe, you can select it like so:
var iframeElements = document.getElementsByTagName("iframe");
And then you can call functions from that:
iframeElements[0].contentWindow.functionName();
From your code, I suppose, that you create an object (called controls) in the global scope of that document, that has the method "reset()". If that is the case,
iframeElements[0].contentWindow.controls.reset();
will do the trick of executing this function.
If you have already tried this, maybe the problem is not how to call the function, but the function itself is doing something that does not work from inside an iframe.
Nope, same domain.
I'll give it a try, thanks.
Thanks Knut. That worked fine!
Thanks guys! This helped.
Hi, this is exactly what I try to do but don't know that much of Javascript. Does the "document" word has to be changed in var iframeElements = document.getElementsByTagName("iframe")?
hi Camille, I was going give you a quick reply but ended up creating a post here: https://community.articulate.com/discussions/articulate-storyline/storyline-javascript-communication-to-the-function-of-a-web-object
Hope the .story file there helps.
Thanks for sharing that Richard, I'm sure it'll help others!
Thank you very much Richard, this is very instructive !
I'll try it on my story.
This discussion is closed. You can start a new discussion or contact Articulate Support.