Calling Javascript function from Flex Application
- Friday, December 19, 2008, 12:57
- Adobe Flex
- Add a comment
This question was raised by one of my friend who needed to call a Javascript function inside flex. Its purely very simple and there are two ways to accomplish it.
1. navigateToURL
2. ExtenalInterface
I prefer the latter one which is relatively pretty simple. The ExternalInterface API has also support for browser checks. Its class has a static method to which you can specify your javascript method you need to call.
call(javascriptFunctionName, ArgumentList, …)
<?xml version=”1.0? encoding=”utf-8??>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” >
<mx:Script>
<![CDATA[import flash.external.*;
import mx.controls.Alert;public function javascriptAlert():void
{
var javascriptFunction:String = "showAlert";
var message:String = messageText.text;
if(ExternalInterface.available)
{
ExternalInterface.call(javascriptFunction, message);
} }]]>
</mx:Script>
<mx:TextInput x=”10? y=”20? text=”Enter Message” id=”messageText”/>
<mx:Button x=”178? y=”20? label=”Call Javascript Function” click=”javascriptAlert()”/></mx:Application>
ExternalInterface.available property is to identify wheather your flash player supports ExternalInterface API or Not.
Javascript in HTML page
<script language=”javascript”>
function javascriptFunction(message)
{
alert(”message from flex :”+ message );
}
</script>
You need to include this javascript into the HTML page.
In above example browser alert will be get displayed with the message you entered in the input box.
Popularity: 1% [?]
About the Author
Write a Comment
Gravatars are small images that can show your personality. You can get your gravatar for free today!