Getting JavaScript variables into your Flex applications using the ExternalInterface API

by Peter deHaan on April 11, 2008

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.

{ 9 comments… read them below or add one }

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

barathrumm September 16, 2008 at 12:37 am

thx saved me a bunch of time =)

Reply

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

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

Stanisalv January 21, 2010 at 6:38 am

it didnot work for me in FF 3.5 also :(

Reply

miChou May 21, 2010 at 1:31 am

@Stanislav, Zeba: IE uses a different tag for embedding Flash contents, <embed/>. See http://www.verticalmoon.com/tutorials/general/flashhtml/flashhtml.htm

Reply

Kenny May 27, 2010 at 10:07 am

Don’t try the clever stuff, they don’t work all the time, especially on IE. Even the unclever stuff don’t work on IE sometimes. IE is just a piece of crap

Reply

Kumar July 9, 2010 at 2:06 am

I took the same mxml file and ran.
On IE 6 its working … But Its failing If I run this example in Firefox.
Following is the error its showing up.
SecurityError: Error #2060: Security sandbox violation: ExternalInterface caller file:///D:/Flex/FindBrowser/bin-debug/FindBrowser.swf cannot access file:///D:/Flex/FindBrowser/bin-debug/FindBrowser.html.
at flash.external::ExternalInterface$/_initJS()
at flash.external::ExternalInterface$/call()
at FindBrowser/init()[D:\Flex\FindBrowser\src\FindBrowser.mxml:14]
at FindBrowser/___FindBrowser_Application1_creationComplete()[D:\Flex\FindBrowser\src\FindBrowser.mxml:7]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()[C:\autobuild\3.5.0\frameworks\projects\framework\src\mx\core\UIComponent.as:9440]
at mx.core::UIComponent/set initialized()[C:\autobuild\3.5.0\frameworks\projects\framework\src\mx\core\UIComponent.as:1168]
at mx.managers::LayoutManager/doPhasedInstantiation()[C:\autobuild\3.5.0\frameworks\projects\framework\src\mx\managers\LayoutManager.as:718]
at Function/http://adobe.com/AS3/2006/builtin::apply()
at mx.core::UIComponent/callLaterDispatcher2()[C:\autobuild\3.5.0\frameworks\projects\framework\src\mx\core\UIComponent.as:8744]
at mx.core::UIComponent/callLaterDispatcher()[C:\autobuild\3.5.0\frameworks\projects\framework\src\mx\core\UIComponent.as:8684]

Please help me out.

Reply

Peter deHaan July 9, 2010 at 4:58 pm

@kumar,

Try running the example from an HTTP:// address rather than file:/// address.

Peter

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: