Here we use the simplest code to demonstrate how to call the JavaScript code in the host HTML in the as3 code.
Mxml code: <? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" xmlns = "*"
Creationcomplete = "oncreationcomplete ()">
<Mx: script source = "externalinterfaceasjs. As"/>
<Mx: canvas width = "100%" Height = "100%">
<Mx: panel X = "0" Y = "0" Height = "100" width = "500" Title = "externalinterface: ActionScript to JavaScript">
<Mx: canvas Height = "100%" width = "100%">
<Mx: button x = "346" Y = "11" label = "send message to JavaScript" id = "submitbutton"/>
<Mx: textinput x = "5" Y = "11" width = "334" id = "tinput"/>
</MX: canvas>
</MX: Panel>
</MX: canvas>
</MX: Application>
As3 code:
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
...{
// Check whether the available attribute of externalinterface is true.
// If the player runs in an independent Flash Player, false is returned for this attribute.
If (externalinterface. available)
...{
VaR S: String = tinput. text;
// Call JavaScript Functions
Externalinterface. Call ("displaystring", S );
}
}
JavaScript code in HTML: <script language = "JavaScript">
// This function will be called by flash
Function displaystring (s)
...{
Alert ("from FLASH:" + S );
}
</SCRIPT>