How to transfer Array within an Object to JavaScript using External Interface ?

November 4th, 2011 by aabhaanjan
Here is an example :
Flex Side :
<?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”;
_obj.familly = new Array(‘RP’,’AK’,’SK’,’BK’);
msg = ExternalInterface.call(‘receiveComplexDataTypes’, _obj);
} else {
msg = “Not available”;
}
}
]]></fx:Script>
<s:Button label=”Send Data” click=”callJavaScriptFunction()”/>
</s:Application>
HTML Side :-
<!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) {
var str = (“The Data values are :- ” + object.fname + ” \n and :-” + object.lname + “!”);
for (i=0; i<object.familly.length; i++) {
str +=  i + “: ” + object.familly [i] + “\n”;
}
alert(str);
return “successful”;
}
</SCRIPT>
<body>
<div id=”flashContent”/>
</body>

Here is an example :

Flex Side :

[xml]

<?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”;

_obj.familly = new Array(‘RP’,’AK’,’SK’,’BK’);

msg = ExternalInterface.call(‘receiveComplexDataTypes’, _obj);

} else {

msg = “Not available”;

}

}

]]></fx:Script>

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

</s:Application>

[/xml]

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) {

var str = (“The Data values are :- ” + object.fname + ” \n and :-” + object.lname + “!”);

for (i=0; i<object.familly.length; i++) {

str +=  i + “: ” + object.familly [i] + “\n”;

}

alert(str);

return “successful”;

}

</SCRIPT>

<body>

<div id=”flashContent”/>

</body>

[/xml]

Tags: , , , , , , , ,