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




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()
thx saved me a bunch of time =)
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”.