Blog
0

Loading a .js File into Flash and Executing it in Actionscript 3.0

posted on   July 25, 2012

Project: Springboard Overlay Unit (author Marko Ristic)

Request:  Create a Flash Overlay unit on top SpringBoard Media Player

Tech: AS3, Flash, JavaScript

Problem: Load and execute a .js file that places the Overlay over the player, and execute it in ActionScript 3.0

Solution: How to execute a medium to complex JavaScript code from Flash without that Javascript actually being embedded into the page that contains the Flash unit…

Here you can find a great tutorial that explains how to execute JS and VB scripts from ActionScript by wrapping them into XML. I suggest you read it before continuing with this text.

Would be even better if you could load a .js file that contains your script into Flash and execute it from ActionScript. Well first load the .js file using the URLLoader class the classical way. When the .js is loaded take the content URLLoader(e.target).data  and place it some String variable. Then similarly to the tutorial on actionscript.org site vreate an XML but put it in a new XML() as a string. Place the variable inside the that xml string like this:

var jsXML:XML = new XML(‘<script><![CDATA[function(params){‘+jsData+’calledFunct(params);}]]> </script>’);

calledFunct(params) is an actual function inside the JavaScript that will be called upon execution. So it’s mandatory and can be named as you like. This also grants you the possibility to call any function inside that JavaScript by just putting the name of that function instead of the calledFunct. To execute the JavaScript use the ExternalInterface.call():

ExternalInterface.call(jsXML, params);

posted in   Uncategorized | No Replies

Leave a Reply

Your email address will not be published. Required fields are marked *