Saturday, 1 September 2012

Call Javascript function from Actionscript 3

In this quick tip I will use the ExternalInterface class to call a Javascript function using Actionscript 3. The ExternalInterface class enables communication between Actionscript and the SWF file within a HTML page. The call() method is used to call a Javascript function from Actionscript. Below is a simple example. I have three buttons on the stage. When a button is clicked the method is called passing the Javascript function and the optional argument. Each button passes a different argument. Note that you can pass any number of arguments and pass different data types.


button1.addEventListener(MouseEvent.CLICK, clicked);
button2.addEventListener(MouseEvent.CLICK, clicked);
button3.addEventListener(MouseEvent.CLICK, clicked);

function clicked(e:MouseEvent):void
 if (ExternalInterface.available) 
  if( == ‘button1’)"callJSFunc", 1);
  if( == ‘button2’)"callJSFunc", 2);
  if( == ‘button3’)"callJSFunc", 3);

<!DOCTYPE html>
<html lang="en">
 <meta charset="utf-8">
 <title>Call Javascript function from Actionscipt</title>
 <script src=""></script>
 <div id="flashContent"></div>
  var flashvars ={};
  var params = {allowscriptaccess:"always"}; 
  var attributes = {};
  swfobject.embedSWF("calljavascript.swf", "flashContent", "550", "400", "10", false, flashvars, params, attributes);
  function callJSFunc(num)
   console.log('The passed number is: ', num);


  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP