====== Getting Return Values from Function Calls ====== {{tag>flashjs tutorial javascript}} This page shows a simple example of how to get return values from JavaScript when calling a JavaScript function from Flash. The same technique works when getting return values from Flash when calling a Flash function from JavaScript, but you just reverse the function order. //import proxy class import com.macromedia.javascript.JavaScriptProxy; class MyClass { private var proxy:JavaScriptProxy; function MyClass() { //create instance of proxy to JavaScript, pass in args, since //we will receive calls from JS proxy = new JavaScriptProxy(_root.lcId, this); sayHello(); } //Calls JavaScript sayHello function function sayHello():Void { proxy.call("sayHello", "Homer"); } //This gets called when a value is returned from the sayHello function in //JavaScript function sayHelloReturn(result:String):Void { trace("Returned from JavaScript : " + result); //traces 'Hello Homer !' } } HTML / JavaScript Code ---- You can also make the return function dynamic, like this: ActionScript code //import proxy class import com.macromedia.javascript.JavaScriptProxy; class MyClass { private var proxy:JavaScriptProxy; function MyClass() { //create instance of proxy to JavaScript, pass in args, since //we will receive calls from JS proxy = new JavaScriptProxy(_root.lcId, this); } function echo(callBack:String, result:String):Void { proxy.call(callBack, result); } } HTML / JavaScript Code ---- Of course, in the ActionScript code, you could handle the return function in a number of ways, such as broadcasting an event when the value is returned like so: ActionScript Code //import proxy class import com.macromedia.javascript.JavaScriptProxy; import mx.events.EventDispatcher; class MyClass { private var proxy:JavaScriptProxy; //declare EventDispatcher functions private var dispatchEvent:Function; public var addEventListener:Function; public var removeEventListener:Function; function MyClass() { //initialize class to dispatch events mx.events.EventDispatcher.initialize(this); //create instance of proxy to JavaScript, pass in args, since //we will receive calls from JS proxy = new JavaScriptProxy(_root.lcId, this); sayHello(); } //Calls JavaScript sayHello function function sayHello():Void { proxy.call("sayHello", "Homer"); } //This gets called when a value is returned from the sayHello function in //JavaScript function sayHelloReturn(result:String):Void { //dispatch event that javascript function has returned dispatchEvent({target:this, type:"onSayHelloReturn", result:result}); } } You can view another example [[projects:flashjs:tutorials:jsinfo2flash|here]].