MXML:
<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*"
creationComplete="onCreationComplete()">

<mx:Script source="ExternalInterfaceJSAS.as" />

<mx:Canvas width="100%" height="100%">
<mx:Panel x="0" y="0" height="300" width="500" title="ExternalInterface : JavaScript to Flash">
<mx:Canvas height="100%" width="100%">
<mx:TextArea x="8" y="9" width="464" height="250" id="tArea"/>
</mx:Canvas>
</mx:Panel>
</mx:Canvas>
</mx:Application>
ExternalInterfaceJSAS.as:

import flash.events.MouseEvent;
import flash.external.ExternalInterface;
import flash.system.Security;

private function onCreationComplete():void

...{
Security.allowDomain("*");
submitButton.addEventListener("click", onSubmitClick);
}

private function onSubmitClick(event:MouseEvent):void

...{
//we need to make sure that ExternalInterface is available
//If you are running in StandAlone player, then this will return false
if(ExternalInterface.available)

...{
var s:String = tInput.text;
//call the javascript function
ExternalInterface.call("displayString", s);
}
}
HTML:
<html>
<head>
<title>ExternalInterfaceJSAS</title>

<style>...

body {...}{ margin: 0px;
overflow:hidden }
</style>


<script language="JavaScript">...

//called when send button is pressed
function doSubmit()

...{
//get the data from the TextArea
var ta = document.getElementById("outputField");
sendToFlash(ta.value);
}

function sendToFlash(input)

...{
//get access to the flash player in the page with an ID of fp
var flash = (navigator.appName.indexOf ("Microsoft") !=-1)?window["fp"]:document["fp"];
//call the function in flash
flash.displayString(input);
}
</script>


</head>
<body scroll='no'>
<object id='fp'
classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'
codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,5,0,0'
height='300' width='450'>
<param name='flashvars' />
<param name='src' value='ExternalInterfaceJSAS.swf'/>
<embed name='fp'
pluginspage='http://www.macromedia.com/go/getflashplayer'
allowScriptAccess='always'
src='ExternalInterfaceJSAS.swf' height='300'
width='500' />
</object>

<form>
<textarea id="outputField" style="width:450px;height:50px"></textarea>
<br />
<input type="button" value="Send to Flash" onclick="doSubmit()"/>
</form>
</body>
</html>
需要注意的地方是不能直接打开html,需要放在服务器上运行,如Tomcat上















ExternalInterfaceJSAS.as:



























HTML:



























































需要注意的地方是不能直接打开html,需要放在服务器上运行,如Tomcat上