Flex is great! Javascript can be great! What if two great things got together?
This is a simple code hint which shows how to call javascript functions from Flex and pass data back and forth:
MXML Button:
<mx:Button
id = "button"
label = "Click Me!"
click = "handleButtonClick(event:MouseEvent)
/>
Flex Function:
private function handleButtonClick(event:MouseEvent):void
{
var functionName:String = "handleButtonClick";
var returnData:String = ExternalInterface.call(f, "You clicked the button!");
}
Javascript Function:
function handleButtonClick(message)
{
alert(message);
}
That’s really all there is to it. the ExternalInterface.call() handles everything for you.
By the way, if you wanted to actually return data from the javascript function, you’d just do a standard return and it’d be available in the returnData variable in Flex:
function handleButtonClick(message)
{
alert(message);
return "success";
}

Related Articles
1 user responded in this post
[...] change the SWF Object as needed based on events being listened to inside that same SWF Object. See http://reynacho.com/2008/08/12/flex-calls-to-javascript/ for a tutorial on how to get Flex to call javascript [...]
Leave A Reply