Share on Google+Share on Google+

Calling Anonymous JavaScript Function From Flex

Advertisement
In flex application, you can call javascript function defined in html file where your flex file is embed.

Calling Anonymous JavaScript Function From Flex

In flex application, you can call javascript function defined in html file where your flex file is embed. But you can also call an anomymous javascript function defined in ActionScript file.

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

<mx:Script>

<![CDATA[

import mx.controls.Alert;

import flash.external.*;

private function callJS():void {

if (ExternalInterface.available) {

var returnFromFunctionCall:String = (ExternalInterface.call("function() { alert('Anonumous function call.'); return 'success' }"));

Alert.show("JavaScript Function returned String: "+returnFromFunctionCall);

} else {

Alert.show("Failed.");

}

}

]]>

</mx:Script>

<mx:Button label="Call JavaScript Anonymous Function" click="callJS()"/>

</mx:Application>

Running the application presents output as below:

Clicking on the button calls javascript function which creates alert box as below:

The function returns value 'success'. Click OK button. You will see the output as below:

Calling Flex Function From JavaScript
Sometimes, you may need to call flex function from JavaScript to pass value in the flex application
 
JavaScript call from flex application
In flex application, you may have called function defined in action script
 

Advertisements

Posted on: July 13, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.