Getting JavaScript variables into your Flex applications using the ExternalInterface API

by Peter deHaan on April 11, 2008

in ExternalInterface

The following example shows an easy way to get JavaScript variables into your Flex applications without having to write a single line of JavaScript or edit your HTML template using the Flash Player ExternalInterface API.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/04/11/getting-javascript-variables-into-your-flex-applications-using-the-externalinterface-api/ -->
<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 {
                var userAgent:String = ExternalInterface.call("navigator.userAgent.toString");
                Alert.show(userAgent, "navigator.userAgent:");
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Button label="Display user agent"
                click="init();" />
    </mx:ApplicationControlBar>

</mx:Application>

View source is enabled in the following example.

{ 5 comments… read them below or add one }

1 lakshmikanth reddy August 5, 2008 at 11:26 pm

i am getting the following error if i try to run this application , how to overcome it ?

SecurityError: Error #2060: Security sandbox violation: ExternalInterface caller file:///C:/FlexProjects/javascript_FLEX/bin/javascript_FLEX.swf cannot access file:///C:/FlexProjects/javascript_FLEX/bin/javascript_FLEX.html.
at flash.external::ExternalInterface$/_initJS()
at flash.external::ExternalInterface$/call()
at javascript_FLEX/init()[C:\FlexProjects\javascript_FLEX\javascript_FLEX.mxml:14]
at javascript_FLEX/___javascript_FLEX_Application1_creationComplete()[C:\FlexProjects\javascript_FLEX\javascript_FLEX.mxml:7]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\core\UIComponent.as:9051]
at mx.core::UIComponent/set initialized()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\core\UIComponent.as:1167]
at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\managers\LayoutManager.as:698]
at Function/http://adobe.com/AS3/2006/builtin::apply()

Reply

2 barathrumm September 16, 2008 at 12:37 am

thx saved me a bunch of time =)

Reply

3 barathrumm November 27, 2008 at 5:11 am

hey lakshmikanth reddy

In the HTML tag that embeds your SWF, check its PARAM tag for the property -
allowScriptAccess. It should not have a “never” as a value. It can have
something like “sameDomain”.

Reply

4 Zeba April 30, 2009 at 3:30 am

I tried running the above example..and it worked fine….
But when I integrated the .swf in an .html file within the <object> tag as:

<object id="swfembed" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="100%" height="100%">
			<param name="movie" value="bin/testbanner.swf" />
			<param name="quality" value="high" />
			<param name="allowScriptAccess" value="always" />
			<param name="wmode" value="opaque"/>
			<embed id="swfembed"
                          name="swfembed"
                          src="bin/testbanner.swf"
                          quality="high"
                          pluginspage="http://www.macromedia.com/go/getflashplayer"
                          allowScriptAccess="always"
                          wmode="transparent"
                          quality="high"
                          width="100%"
                          height="100%">
                        </embed>
</object>

It worked fine with Mozilla but not in IE6/7!! :(
What needs to be done???

Reply

5 Stanisalv January 21, 2010 at 6:38 am

it didnot work for me in FF 3.5 also :(

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; .

You can 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

Previous post:

Next post: