Archive for the ‘External Interface’ Category

Calling Flex Function From JavaScript

November 8th, 2011 by aabhaanjan
Here is an example :
Flex Side :
[xml]
<?xml version=”1.0″?>
<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” creationComplete=”initApp()”>
<fx:Script>
import flash.external.*;
import mx.controls.Alert;
import flash.events.*;
private var alert:Alert;
public function initApp():void {
try {
ExternalInterface.addCallback(“myFlexFunction”,myFunc);
} catch(e:Error) {
trace(e)
}
alert = Alert.show(‘App is initialized ‘);
}
public function myFunc(msg:String):void {
alert = Alert.show(‘JavaScript is sending value to As \n ‘+msg);
}
</fx:Script>
</s:Application>
[/xml]

Here is an example :

Flex Side :

[xml]

<?xml version=”1.0″?>

<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” creationComplete=”initApp()”>

<fx:Script>

import flash.external.*;

import mx.controls.Alert;

import flash.events.*;

private var alert:Alert;

public function initApp():void {

try {

ExternalInterface.addCallback(“myFlexFunction”,myFunc);

} catch(e:Error) {

trace(e)

}

alert = Alert.show(‘App is initialized ‘);

}

public function myFunc(msg:String):void {

alert = Alert.show(‘JavaScript is sending value to As \n ‘+msg);

}

</fx:Script>

</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>addCallback() Wrapper</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 = “ExternalInterfaceAddCallback”;
attributes.name = “ExternalInterfaceAddCallback”;
attributes.align = “middle”;
swfobject.embedSWF(
“ExternalInterfaceAddCallback.swf”, “flashContent”,
“100%”, “100%”,
swfVersionStr, xiSwfUrlStr,
flashvars, params, attributes);
</script>
</head>
<SCRIPT LANGUAGE=”JavaScript”>
function callApp() {
window.document.title = document.getElementById(“myName”).value;
var ExternalInterfaceAddCallback = document.getElementById(“ExternalInterfaceAddCallback”);
ExternalInterfaceAddCallback.myFlexFunction(window.document.title);
}
</SCRIPT>
<body>
<form id=”f1″>
Enter Your Name: <input type=”text” size=”30″ id=”myName” onchange=”callApp()”>
</form>
<div id=”flashContent”/>
</body>
</html>
[/xml]

Tags: , , , ,

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: , , , , , , , ,

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: , , , , , , , , , ,

ExternalInterface and Flex

November 3rd, 2011 by aabhaanjan
ExternalInterface and Flex :
The ExternalInterface API enables a communication between SWF and JavaScript.
Here is an simple example :

The ExternalInterface API enables a communication between SWF and JavaScript.

Here is an simple 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>

import flash.external.ExternalInterface;

public function callASFunction():void {

var msg:String;

if (ExternalInterface.available) {

msg = ExternalInterface.call(‘javaScriptFunction’,’ Communication between JS and App done’);

} else {

msg = “Not available”;

}

}

</fx:Script>

<s:Button label=”Show Output” click=”callASFunction()”/>

</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”>

<!– saved from url=(0014)about:internet –>

<html xmlns=”http://www.w3.org/1999/xhtml” lang=”en” xml:lang=”en”>

<head>

<title>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 = “ExternalInterfaceExample”;

attributes.name = “ExternalInterfaceExample”;

attributes.align = “middle”;

swfobject.embedSWF(

“ExternalInterfaceExample.swf”, “flashContent”,

“100%”, “100%”,

swfVersionStr, xiSwfUrlStr,

flashvars, params, attributes);

</script>

</head>

<SCRIPT LANGUAGE=”JavaScript”>

function javaScriptFunction(msg) {

alert(msg);

return “successful”;

}

</SCRIPT>

<body>

<div id=”flashContent”/>

</body>

</html>

[/xml]

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