Search code examples
javascriptactionscript-3callbackexternalinterface

return value from flash to javascript


How can I return a value from AS3 to javascript. I am calling a AS3 method from JS and want that AS3 method to return back a string:

//javascript
var string = swfObject["abcmethod"](arg1);

The only way I know of is to add a callback method which is called from flash back in JS. Is there a better way?


Solution

  • The communication pipe between AS3 and Javascript does support passing a value back to the Javascript function called. It's in the docs:

    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html

    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.

    If the code you have isn't working, try using a different syntax. The following is what I usually use:

    var myFlashObject = document.getElementById("theIdYouSuppliedToSwfObject");
    var myResult = myFlashObject.abcmethod(arg1);
    

    Your code in actionscript should be something like:

    ExternalInterface.addCallback("abcmethod", onAbcMethodCall);
    private function onAbcMethodCall(...args:Array):String
    { return "Hello World"; }