MXML:<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*"
creationComplete="onCreationComplete()">
<mx:Script source="ExternalInterfaceASJSReturn.as" />
<mx:Panel height="300" width="500" title="ExternalInterface : Returning a value to ActionScript from JavaScript">
<mx:Canvas height="100%" width="100%">
<mx:Button y="6" label="Get Browser Info" id="submitButton" width="118" x="180">
</mx:Button>
<mx:TextArea x="5" y="34" width="468" height="228" id="tArea"/>
</mx:Canvas>
</mx:Panel>
</mx:Application>
ExternalInterfaceASJSReturn.as:
import flash.external.ExternalInterface;
import flash.events.MouseEvent;
private function onCreationComplete():void
...{
submitButton.addEventListener("click", onSubmitClick);
}
private function onSubmitClick(event:MouseEvent):void
...{
if(ExternalInterface.available)
...{
//call the JavaScript function, and store the return value
//in a variable
var info:Object = ExternalInterface.call("getBrowserInfo");
var sb:String = null;
//loop through the results
for(var x:String in info)
...{
sb = sb + x + " : " + info[x] + " ";
}
//print them out to the TextArea
tArea.text = sb.toString();
}
}
JavaScript:function getBrowserInfo()
...{
var docElement = document.documentElement;
var o = new Object();
o.href = location.href;
o.lang = docElement.lang;
o.offsetTop = docElement.offsetTop;
o.offsetLeft = docElement.offsetLeft;
o.offsetWidth = docElement.offsetWidth;
o.offsetHeight = docElement.offsetHeight;
o.scrollTop = docElement.scrollTop;
o.scrollLeft = docElement.scrollLeft;
o.scrollHeight = docElement.scrollHeight;
o.scrollWidth = docElement.scrollWidth;
o.clientHeight = docElement.clientHeight;
o.clientWidth = docElement.clientWidth;
o.width = document.width;
o.height = document.height;
o.domain = document.domain;
o.lastModified = document.lastModified;
return o;
}