Call a JavaScript function from Flex App.Using ExternalInterface API

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&#8221; 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.

9 Responses to Call a JavaScript function from Flex App.Using ExternalInterface API

  1. Pradip Jadhav says:

    Hi Kumar,

    I have one problem, i want to use Iframe in my flex application to call many JSP files inside that Iframe. I written java script code in html file which is inside bin-debug folder and its working fine.

    Now problem is that when i clean or compile project, then old HTML file is get deleted and it will create new HTML file bcoz of this i am not getting my old JavaScript code in HTML file.

    So will u plz tell me the solution for this………..?

    Thanx in advance….. 🙂

    Regards,
    Pradip Jadhav

  2. kumargandhi says:

    Hi Pradip,

    The HTML files in the bin-debug or bin-release are created using the template files in the html-template folder of your project, so every time you compile,build or clean the project,the template files are used to create a new set of HTML files to the corresponding.So the solution is , what ever code you want to include in the HTML files just include them in the template files as they are used for creating a new ones.The file in the html-template folder used to create the App HTML file is index.template.html, so make changes to this file,not to the APP html files in the bin-debug folder or bin-release folder. Hope this helps you.

    regards,
    kumar.

  3. […] this Post on the […]

  4. Tejal says:

    Hi
    I calling one JSP page from flex application using ExternalInterface. The External interface is mostly simmillar to Change Password window in any application along with “Change Password” button.

    In that JSP page one .js file has been included which is having one javascript function to close that window after performing change password operation.

    When I run the flex application and get the extrenal interface of change password , after entering the required fields when i click on Change password button somewhere the .js file is not getting called.
    Is there any solution for this ? ..

    When I run the external interface without flex, it workd properly means on clickc of change password it shows one window from where user can close the window.

  5. Tejal says:

    is it possible to capture java script event from flex application ?

  6. Tejal says:

    The problem is Iframe does not supports any event that are inside theHTML which is loaded in it. because of this we would not be able to capture the event of external interface.

    You can refer the following link for more details http://www.w3schools.com/TAGS/tag_iframe.asp

  7. Tejal says:

    I am facing one issue related to Time Zone.
    There is one screen which is having one datagrid , and one of that column is showing some details regarding time in “HR:MM” format.

    This value is coming from server for which time zone is EST and it is binded with Date dat type in Value object(both flex and java end).

    If try to see the same screen by setting the other time zone say IST , the value for the tSent time column is coming as differnt . Say server is sending me time as 14:05 then on screen I am getting “00:35” i e. 2.30 hrs difference.

    Technical details :
    1> using format(Date) method of DateFormatter to get time in “JJ:NN” format.
    2>format () method is internally calling formatValue() method which is not retruning the proepr value for time.

    Is there any solution to handle this issue ? Means if client is accessing the application from any location then he should get the same server time only.

    any framework class available in Flex SDK 3 and/or new Flex SDK 4 for such issue.? any idea?

Leave a comment