Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: mx = "http://www.adobe.com/2006/mxml" layout = "absolute">
<Mx: Script>
<! [CDATA [
Import flash. external. ExternalInterface;
Public function invokeJavaScript (): void
{
LblResult. text = ExternalInterface. call ("returnTheName", txtName. text );
}
]>
</Mx: Script>
<Mx: Panel width = "800" height = "600" title = "ActionScript calls JavaScript" verticalCenter = "0" horizontalCenter = "0">
<Mx: TextInput id = "txtName" text = "[enter a name]"/>
<Mx: Button id = "btnConnect" label = "OK" click = "invokeJavaScript ()" x = "229" y = "204"/>
<Mx: Label id = "lblResult" text = "test" x = "205" y = "99" width = "150" height = "80"/>
</Mx: Panel>
</Mx: Application>
Add the function returnTheName (a) {return "success! "+ };
The above is the method in js called by.
The following describes how to call the as in js.
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: mx = "http://www.adobe.com/2006/mxml" creationComplete = "init ()" layout = "absolute">
<Mx: Script>
<! [CDATA [
Import flash. external. ExternalInterface;
Private function init (): void
{
ExternalInterface. addCallback ("addTo100", add); // declare a method that can be called
}
Private function add (): int
{
Var I: int;
Var sum: int = 0;
For (I = 1; I <100; I ++)
{
Sum + = I;
}
Return sum;
}
]>
</Mx: Script>
</Mx: Application>
Add <button onclick = "addMethod ()"> click to call </button> to the html page.
Add the function addMethod {var s = JavaScriptCallActionScript. addTo100 (); alert (s);} to the js file );}