Its greate Experience working with addCallBack method and finally I got it working. I tried various ways. First I simply register the method in flex using addCallBack function but I didnt succeeded. After looking through live docs and reading more about it I come know swf initiates the and talks with browser that swf is ready to accept the call from HTML pages. How did I achieve this?
Here is my Mxml code
MXML Application:
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” creationComplete=”initApp();”>
<mx:Script>
<![CDATA[
import flash.external.*;
import mx.controls.Alert;
public var readyTimer:Timer;
public var javascriptMethodCounter:int = 0;
public function initApp():void
{
if (isContainerReady())
{
// If the container is ready, register the SWF's functions.
setUpCall();
}
else
{
/* If the container is not ready, set up a Timer to call the
container at 100ms intervals. Once the container responds that
it's ready, the timer will be stopped. */
readyTimer = new Timer(100);
readyTimer.addEventListener(TimerEvent.TIMER, timerHandler);
readyTimer.start();
}
}
public function showAlert(message:String):void
{
javascriptMethodCounter++;
javascriptCounterText.text = javascriptMethodCounter + " times javascript function called ";
javascriptMessageLogger.text = message;
}
public function timerHandler():void
{
if (isContainerReady())
{
// If the container is ready, register the SWF's functions.
setUpCall();
readyTimer.stop();
}
}
//setup the callback function so that HTML can call flex methods
public function setUpCall():void
{
ExternalInterface.addCallback("showAlert",showAlert);
}
//Checks wheather HTML page is loaded or not.
private function isContainerReady():Boolean
{
var result:Boolean = ExternalInterface.call("isReady");
Alert.show("Result :"+result)
return result;
}
]]>
</mx:Script>
<mx:Panel x=”10″ y=”10″ width=”386″ height=”200″ layout=”absolute”>
<mx:TextArea x=”10″ y=”36″ width=”346″ height=”114″ id=”javascriptMessageLogger”/>
<mx:Label x=”10″ y=”10″ text=”times javascript function called” width=”260″
id=”javascriptCounterText”/>
</mx:Panel>
</mx:Application>
Above code comprises of following method :
initApp : This methods will get call on the creationComplete of an event. This checks wheather swf is ready for
to accept call from HTML/javascript or not.
isContainerReady: this simply call one javascript function which returns boolean value.
timerHandler: after calling isContainerReady function if result if false, we register a timer which gets call
every 100ms to register the swf communication. When successful timer get stoped and call setUpCall method.
setUpCall: Simply registers the Flex method with ExternalInterface addCallBack method.
showAlert: This is the method which we are registered to be callable via HTML
Controls :
javascriptMessageLogger : Displays the message passed via javascript method
javascriptCounterText: Shows the number of times methods get called from javascript.
HTML code:
<script language=”javascript”>
var interfaceObj;
function callFlexMethod()
{
if(navigator.appName.indexOf(“microsoft”)!=-1)
{
alert(“Microsoft”);
interfaceObj = window["ExternalInterface1"];
}
else
{
interfaceObj = document["ExternalInterface1"];
}
}
// ——- Private vars ——-
var jsReady = false;
// ——- functions called by ActionScript ——-
// called to check if the page has initialized and JavaScript is available
function isReady() {
return jsReady;
}
// called by the onload event of the <body> tag
function pageInit() {
// Record that JavaScript is ready to go.
jsReady = true;
callFlexMethod();
}
</script>
isReady : Simply returns the state of jsReady variable
pageInit: this method get called on onload event of page. This function makes jsReady true and get the copy of swf object into interfaceObj variable
callFlexMethod: Determines the browser type and get the proper object out of it
Form Element:
Enter Text : <input type=”text” id=”myText”><input type=”button” value=”Send to Flex” onclick=”interfaceObj.showAlert(document.getElementById(‘myText’).value);”>
myText: This is where we enter the message we want to passed to flex method.
Button : Onclick of button showAlert method of flex application get call with value from textInput.
I was able to successfully makes flex methods available to javascript and call it from javascript. One more thing it should be run from webserver.
