10
Mar
08

Checking to see if the ExternalInterface API is available in Flex

The following example shows how you can check to see whether your Flex application is in a container that supports the ExternalInterface API.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/03/10/checking-to-see-if-the-externalinterface-api-is-available-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();">

    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;

            private function init():void {
                if (ExternalInterface.available) {
                    ExternalInterface.call("alert",
                            "ExternalInterface is available");
                } else {
                    Alert.show("ExternalInterface is not available");
                }
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Button id="button"
                label="ExternalInterface.available"
                click="init();" />
    </mx:ApplicationControlBar>

</mx:Application>

View source is enabled in the following example.


6 Responses to “Checking to see if the ExternalInterface API is available in Flex”


  1. 1 Klaus Busse Mar 11th, 2008 at 3:36 am

    Mmmh, disabling Javascript should disable the external interface, right?

    However, if I do so, I don’t get the Flex Alert. Of course I also don’t get the Javascript alert, too. Have I got this wrong or is there an issue with the example?

    Thanks!

    Klaus

  2. 2 peterd Mar 11th, 2008 at 8:42 am

    Klaus,

    As it turns out, I was unclear/confused on what the ExternalInterface.available property actually does.

    From the docs:

    Note: The ExternalInterface.available property reports whether the current container is a type that supports ExternalInterface connectivity. It will not tell you if JavaScript is enabled in the current browser.

    I’ll try and update the entry later today and find a better solution (since as you point out, the Flex Alert is pretty much impossible). :)

    Peter

  3. 3 peterd Mar 11th, 2008 at 3:17 pm

    OK, I was thinking of this problem over lunch and think I have two possibilities:
    1) In your HTML template (/html-template/index.template.html), pass a variable “jsEnabled” using FlashVars in the <noscript /> block. Then, in your Flex application, check for the existance of the jsEnabled variable in Application.application.parameters. If it exists, the <noscript /> block embedded the SWF and the user’s JavaScript is disabled.
    2) In your Flex app, try calling ExternalInterface.call(”Date”). If JavaScript is enabled and ExternalInterface is working, you should get the current date/time from JavaScript. If JavaScript is disabled or ExternalInterface is not available, you should get “null” back.

    I’ll do a bit more testing after work tonight and update the entry.

    Sorry again for the confusion,
    Peter

  4. 4 Pagerank Jun 5th, 2008 at 8:20 pm

    Popup worked, thanks for sharing the information :-)

  5. 5 VERSANDAPOTHEKE Jul 10th, 2008 at 7:25 pm

    Seems to be working fine. Looks pretty easy to work with.

  6. 6 Rene Weller Jul 11th, 2008 at 6:54 am

    worked for me!

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".