Checking to see if the ExternalInterface API is available in Flex

by Peter deHaan on March 10, 2008

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.

{ 7 comments… read them below or add one }

Klaus Busse March 11, 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

Reply

peterd March 11, 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

Reply

peterd March 11, 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

Reply

Pagerank June 5, 2008 at 8:20 pm

Popup worked, thanks for sharing the information :-)

Reply

VERSANDAPOTHEKE July 10, 2008 at 7:25 pm

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

Reply

Rene Weller July 11, 2008 at 6:54 am

worked for me!

Reply

Apotheke October 7, 2009 at 6:33 am

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

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: