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.

 
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.

9 Responses to Getting JavaScript variables into your Flex applications using the ExternalInterface API

  1. lakshmikanth reddy says:

    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()

  2. barathrumm says:

    thx saved me a bunch of time =)

  3. barathrumm says:

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

  4. Zeba says:

    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???

  5. Stanisalv says:

    it didnot work for me in FF 3.5 also :(

  6. miChou says:

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

  7. Kenny says:

    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

  8. Kumar says:

    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.

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