Posts Tagged ‘addCallback’

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