How to transfer complex data to JavaScript using External Interface ?

November 4th, 2011 by aabhaanjan

Here is an example :
Flex Side :
[java]
<?xml version=”1.0″ encoding=”utf-8″?>
<s:Application
xmlns:fx=”http://ns.adobe.com/mxml/2009″
xmlns:mx=”library://ns.adobe.com/flex/mx”
xmlns:s=”library://ns.adobe.com/flex/spark”>
<s:layout>
<s:HorizontalLayout/>
</s:layout>
<fx:Script><![CDATA[
import flash.external.*;

public function callJavaScriptFunction():void {
var msg:String;
if (ExternalInterface.available) {
var _obj:Object = new Object();
_obj.fname = “Aava”;
_obj.lname = “Rani”;
msg = ExternalInterface.call(‘receiveComplexDataTypes’, _obj);
} else {
msg = “Not available”;
}
}
]]></fx:Script>

<s:Button label=”Send Data” click=”callJavaScriptFunction()”/>

</s:Application>
[/java]
HTML Side :-
[xml]
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” lang=”en” xml:lang=”en”>
<head>
<title>Complex Data and ExternalInterface Example</title>
<script type=”text/javascript” src=”swfobject.js”></script>
<script type=”text/javascript”>
var swfVersionStr = “0”;
var xiSwfUrlStr = “”;
var flashvars = {};
var params = {};
params.quality = “high”;
params.bgcolor = “#ffffff”;
params.allowscriptaccess = “sameDomain”;
var attributes = {};
attributes.id = “ComplexDataExternalInterface”;
attributes.name = “ComplexDataExternalInterface”;
attributes.align = “middle”;
swfobject.embedSWF(
“ComplexDataExternalInterface.swf”, “flashContent”,
“100%”, “100%”,
swfVersionStr, xiSwfUrlStr,
flashvars, params, attributes);
</script>
</head>

<SCRIPT LANGUAGE=”JavaScript”>
function receiveComplexDataTypes(object) {
alert(“The Data values are :- ” + object.fname + ” \n and :-” + object.lname + “!”);
return “successful”;
}
</SCRIPT>

<body>
<div id=”flashContent”/>
</body>
</html>
[/xml]

Tags: , , , , , , , , , ,