Andy Hin
Andy Hin

Reputation: 31883

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?

Upvotes: 4

Views: 3071

Answers (2)

Mansoor Siddiqui
Mansoor Siddiqui

Reputation: 21663

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:

Upvotes: 9

Jacob
Jacob

Reputation: 78850

Use the ExternalInterface object to have your ActionScript be able to call JavaScript functions and vice-versa.

Upvotes: 5

Related Questions