Pages

Thursday, June 21, 2012

Send and Get data from JavaScript in AS3

How to Connect JavaScript in as3?

External Interface:-
      The ExternalInterface class is an application programming interface that enables straightforward communication between ActionScript and the SWF container- for example, an HTML page with JavaScript or a desktop application that uses Flash player to display a SWF file.

Using the ExternalInterface class, you can call an ActionScript function in the Flash runtime, using JavaScript in the HTML page. The ActionScript function can return a value, and JavaScript receives it immediately as the return value of the call.


import flash.external.ExternalInterface;

// calling a javascript function from as3 (with optional parameters)
if(ExternalInterface.available)
ExternalInterface.call("javascriptFunction",param1,param2);
//implementing a javascript callback in as3 (flashFunction is called when Javascript calls javascriptFuntion)
if(ExternalInterface.available)
ExternalInterface.addCallback("javascriptFunction",flashFunction);

Example:

JavaScript code:-

<script>
function jsAdd(value1,value2)
{
       return ('ans:'+value1+value2);
}
</script>

as3 code:-

import flash.external.ExternalInterface;


var output:String=ExternalInterface.call("jsAdd",10,15);
ExternalInterface.addCallback("jsAdd",callJs);


function callJs():void
{
     trace("successfully called js method");
}

...CHeeRS..

0 comments:

Post a Comment