Package {
Import flash. display. Sprite;
Import flash. Events .*;
Import flash. External. externalinterface;
Import flash. Text. textfield;
Import flash. utils. timer;
Import flash. Text. textfieldtype;
Import flash. Text. textfieldautosize;
Public class externalinterfaceexample extends sprite {
Private var input: textfield;
Private var output: textfield;
Private var sendbtn: SPRITE;
Public Function externalinterfaceexample (){
Input = new textfield ();
Input. type = textfieldtype. input;
Input. Background = true;
Input. Border = true;
Input. width = 350;
Input. Height = 18;
Addchild (input );
Sendbtn = new sprite ();
Sendbtn. mouseenabled = true;
Sendbtn. x = input. Width + 10;
Sendbtn. Graphics. beginfill (0 xcccccc );
Sendbtn. Graphics. drawroundrect (0, 0, 80, 18, 10, 10 );
Sendbtn. Graphics. endfill ();
Sendbtn. addeventlistener (mouseevent. Click, clickhandler );
Addchild (sendbtn );
Output = new textfield ();
Output. Y = 25;
Output. width = 450;
Output. Height = 325;
Output. multiline = true;
Output. wordwrap = true;
Output. Border = true;
Output. Text = "Initializing.../N ";
Addchild (output );
If (externalinterface. Available ){
Try {
Output. appendtext ("adding callback.../N ");
Externalinterface. addcallback ("sendtoactionscript", receivedfromjavascript );
If (checkjavascriptready ()){
Output. appendtext ("javascript is ready./N ");
} Else {
Output. appendtext ("javascript is not ready, creating timer./N ");
VaR readytimer: timer = new timer (100, 0 );
Readytimer. addeventlistener (timerevent. Timer, timerhandler );
Readytimer. Start ();
}
} Catch (error: securityerror ){
Output. appendtext ("A securityerror occurred:" + error. Message + "/N ");
} Catch (error: Error ){
Output. appendtext ("an error occurred:" + error. Message + "/N ");
}
} Else {
Output. appendtext ("external interface is not available for this container .");
}
}
Private function receivedfromjavascript (value: string): void {
Output. appendtext ("javascript says:" + value + "/N ");
}
Private function checkjavascriptready (): Boolean {
VaR isready: Boolean = externalinterface. Call ("isready ");
Return isready;
}
Private function timerhandler (Event: timerevent): void {
Output. appendtext ("Checking JavaScript status.../N ");
VaR isready: Boolean = checkjavascriptready ();
If (isready ){
Output. appendtext ("javascript is ready./N ");
Timer(event.tar get). Stop ();
}
}
Private function clickhandler (Event: mouseevent): void {
If (externalinterface. Available ){
Externalinterface. Call ("sendtojavascript", input. Text );
}
}
}
}
In order to test the previous ActionScript code, embed the generated SWF file using the following HTML template:
<! -- Save from url = (0014) about: Internet -->
<HTML lang = "en">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> externalinterfaceexample </title>
<Script language = "JavaScript">
VaR jsready = false;
Function isready (){
Return jsready;
}
Function pageinit (){
Jsready = true;
Document. Forms ["form1"]. Output. Value + = "/N" + "javascript is ready./N ";
}
Function thismovie (moviename ){
If (navigator. appname. indexof ("Microsoft ")! =-1 ){
Return window [moviename];
} Else {
Return document [moviename];
}
}
Function sendtoactionscript (value ){
Thismovie ("externalinterfaceexample"). sendtoactionscript (value );
}
Function sendtojavascript (value ){
Document. Forms ["form1"]. Output. Value + = "ActionScript says:" + value + "/N ";
}
</SCRIPT>
</Head>
<Body onload = "pageinit ();">
<Object classid = "CLSID: D27CDB6E-AE6D-11cf-96B8-444553540000"
Id = "externalinterfaceexample" width = "500" Height = "375"
Codebase = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">
<Param name = "movie" value = "externalinterfaceexample.swf"/>
<Param name = "quality" value = "high"/>
<Param name = "bgcolor" value = "#869ca7"/>
<Param name = "allowScriptAccess" value = "samedomain"/>
<Embed src = "externalinterfaceexample.swf" Quality = "high" bgcolor = "#869ca7"
Width = "500" Height = "375" name = "externalinterfaceexample" align = "Middle"
Play = "true" loop = "false" Quality = "high" allowScriptAccess = "samedomain"
Type = "application/X-Shockwave-flash"
Pluginspage = "http://www.macromedia.com/go/getflashplayer">
</Embed>
</Object>
<Form name = "form1" onsubmit = "Return false;">
<Input type = "text" name = "input" value = ""/>
<Input type = "button" value = "send" onclick = "sendtoactionscript (this. Form. Input. Value);"/> <br/>
<Textarea Cols = "60" rows = "20" name = "output" readonly = "true"> initializing... </textarea>
</Form>
</Body>
</Html>