Getting Return Values from Function Calls
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
<script type="text/javascript" src="/path/to/Exception.js"></script> <script type="text/javascript" src="/path/to/FlashTag.js"></script> <script type="text/javascript" src="/path/to/FlashSerializer.js"></script> <script type="text/javascript" src="/path/to/FlashProxy.js"></script> <script language="JavaScript"> //create unique id var uid = new Date().getTime(); //create proxy to flash var flashProxy = new FlashProxy(uid, '/path/to/JavaScriptFlashGateway.swf'); //this function is called from Flash function sayHello(n) { var returnString = "Hello " + n + "!"; flashProxy.call("sayHelloReturn", returnString); } </script> <script type="text/javascript"> var tag = new FlashTag('/path/to/flashContent.swf', 300, 300); // last two arguments are height and width tag.setFlashvars('lcId='+uid); tag.write(document); </script>
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
<script type="text/javascript" src="/path/to/Exception.js"></script> <script type="text/javascript" src="/path/to/FlashTag.js"></script> <script type="text/javascript" src="/path/to/FlashSerializer.js"></script> <script type="text/javascript" src="/path/to/FlashProxy.js"></script> <script language="JavaScript"> //create unique id var uid = new Date().getTime(); //create proxy to flash var flashProxy = new FlashProxy(uid, '/path/to/JavaScriptFlashGateway.swf'); //this function is called from Flash function sayHello(n) { var returnString = "Hello " + n + "!"; alert(n); } flashProxy.call("echo", "sayHello", "hello"); </script> <script type="text/javascript"> var tag = new FlashTag('/path/to/flashContent.swf', 300, 300); // last two arguments are height and width tag.setFlashvars('lcId='+uid); tag.write(document); </script>
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 here.
projects/flashjs/tutorials/simreturn.txt · Last modified: 2007/02/21 00:33 by niko