Archive for the ‘javascript’ Category

Calling Flex / Actionscript functions from Javascript

May 8, 2012

Nowadays I am working with Flex projects, that runs on Lotus Notes platform, which consumes data from Lotus Notes backend. Since there is no remote services like BlazeDS to connect to Notes server, am now greatly dependant on HTTPService and Javascript. Calling a javascript function from Flex is quite easy. Just use the ExternalInterface API. For those who don’t know the way, this is how it is getting called.

In AS3

if(ExternalInterface.available){“openNotes”, parameter);

In Javascript

function openNotes(notesUrl){, ”, ‘width=1000,height=600’);

It is quite easy. But what if you need to call the reverse. ie, calling actionscript function from javascript. We can use the same ExternalInterface api for achieve this. There is a method called addCallback, available in ExternalInterface. addCallback method registers an ActionScript method as callable from the container. After a successful invocation of addCallBack(), the registered function in the player can be called by JavaScript or ActiveX code in the container. The first parameter is the name by which the container can invoke the function and the second parameter is the function closure to invoke. Below is the step-by-step configuration:

Step 1 : Register the call back from actionscript. For eg, call the below method in the creationComplete or initialize event of the component.

private function createCallBack(event:Event):void{
ExternalInterface.addCallback(“updateNotes”, getNotes);

private function getNotes():void{
//do whatever you want after getting the hold

Step 2 : Create logic in javascript to invoke the AS3 function.

function updateFlex(){

The appName is the name and id of the embedded swf object in the HTML. Like below :

That’s it. Now you can call the updateFlex javascript method from your HTML and it will invoke the AS3 callback function. Enjoy Coding guys. Flex Your Life. Cheers. 🙂

%d bloggers like this: