Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Flash and Javascript communicate?

I am working on a chatting app that makes use of flash sockets. I don't want to code the entire interface in flash, so I was hoping to just use flash to talk to the socketserver and call js to modify the dom.

can this be done?

like image 545
Andy Hin Avatar asked Dec 10 '22 11:12

Andy Hin


1 Answers

To call a JavaScript function from Flash, use the ExternalInterface.call function in ActionScript:

import flash.external.ExternalInterface;

// Call a JavaScript function
ExternalInterface.call("your_javascript_function");

// Get a return value from a JavaScript function
var x:int = ExternalInterface.call("get_x");

// Pass an argument to a JavaScript function
var retval:int = ExternalInterface.call("some_js_function", "the-argument");

To call an ActionScript function from JavaScript, first use the ExternalInterface.addCallback function in ActionScript:

// "methodName" is the method to call in JavaScript
// instanceObject.realMethod is the method that will be triggered
var successful = ExternalInterface.addCallback("methodName",
                                               instanceObject,
                                               realMethod);

Then, get a handle on the SWFObject in JavaScript and call the ActionScript method as follows:

function makeActionScriptCall() {
    var flash = document.getElementById(movieName);
    flash.methodName(parametersIfAny);
}

For more information, see:

  • http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/external/ExternalInterface.html
  • http://codingrecipes.com/calling-a-javascript-function-from-actionscript-3-flash
  • http://bytes.com/topic/flash/answers/694359-how-do-i-access-flash-function-using-javascript
like image 84
Mansoor Siddiqui Avatar answered Dec 21 '22 08:12

Mansoor Siddiqui