11
Apr
08

Getting JavaScript variables into your Flex applications using the ExternalInterface API

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.


3 Responses to “Getting JavaScript variables into your Flex applications using the ExternalInterface API”


  1. 1 lakshmikanth reddy Aug 5th, 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()

  2. 2 barathrumm Sep 16th, 2008 at 12:37 am

    thx saved me a bunch of time =)

  3. 3 barathrumm Nov 27th, 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”.

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".




Badge Farm

  • Firefox 2
  • Powered by Redoable 1.2
  • Feeds burnt by Feedburner
  • Feed