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.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

8 Responses to Checking to see if the ExternalInterface API is available in Flex

  1. Klaus Busse says:

    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. peterd says:

    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. peterd says:

    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. Pagerank says:

    Popup worked, thanks for sharing the information :-)

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

  6. Apotheke says:

    Thanks for the code. I had to adapt it to my problem, but now it works.

  7. Danke für den sehr hielfreichen Code.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree