Call a JavaScript function from Flex App.Using ExternalInterface API

January 1, 2008

Here to do this we have to use the ExternalInterface API.

call() Method of this Class will help doing this.

<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” creationComplete=”init()”>

<mx:Script>
<![CDATA[
private function init():void
{
ExternalInterface.call("alertfromJS");
}
]]>
</mx:Script>

</mx:Application>

Here in the above code we are calling a function in the Javascript of the html page.on initilizing of the App.

The function is alertfromJS which we have to define at the Html page and in the JS block.

And that would be:-

<script language=”JavaScript” type=”text/javascript”>
function alertfromJS()
{
alert(”kumar Gandhi”);
}

</script>

So now when u run the App , a alert box from Javascript will appear with text KumarGandhi.

A good programmer will always have an eye on the html wrapper generated by the Flex App.


Applying styles to Alert Box.[To every Alert Box and to a particular Alert Box]

January 1, 2008

The default style of Alert box in flex is dull in color ,so to change it we have to use the following code.

<mx:Style>
Alert
{
backgroundAlpha: 0.3;
backgroundColor: black;
borderAlpha: 0.3;
borderColor: white;
dropShadowEnabled: false;
}
</mx:Style>

This will apply style to every Alert box in the Flex app, so to apply style to a particularAlert Box the below code will help.

<mx:Script>
<![CDATA[

var alert:Alert;
alert=Alert.show("kumar Gandhi","my Style");
alert.styleName="myalertstyle";

]]>
</mx:Script>

<mx:Style>
.myalertstyle
{
backgroundAlpha: 0.3;
backgroundColor: black;
borderAlpha: 0.3;
borderColor: white;
dropShadowEnabled: false;
}
</mx:Style>

This will give colorfull Alert Box in flex app which will defenetily define  RIA.


using setFocus() method to bring focus to a Textinput control.

January 1, 2008

To bring focus to a Textinput control we have to use some bit of AS.

And the method to be used is setFocus().

EX: textinputreference.setFocus();

This will bring the focus to the Textinput of ID  textinputreference.

The below example explains the need.

<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” creationComplete=”init()”>

<mx:TextInput id=”search” x=”10″ y=”71″/>

<mx:Script>
<![CDATA[
private function init():void
{
search.setFocus();
}
]]>
</mx:Script>

usable method.