Let's take a look at how JavaScript calls functions in flex.
Javascriptconnectflex. mxml <? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute" creationcomplete = "initapp ()">
<Mx: SCRIPT>
<! [CDATA [
Import flash. External. externalinterface;
Public Function add (): int
{
VaR I: int;
VaR sum: Int = 0;
For (I = 1; I <= 100; I ++)
{
Sum + = I;
}
Return sum;
}
Public Function initapp (): void
{
Externalinterface. addcallback ("addto100", add );
}
]>
</MX: SCRIPT>
</MX: Application>
Added to javascriptconnectflex.html
<Button onclick = "addmethod ()"> calculation </button>
...
Join
Function addmethod ()
{
VaR S = javascriptconnectflex. addto100 ();
Alert (s );
}
///////////////
Summary
1. externalinterface. addcallback ("addto100", add); defines the methods that javascript can call.
2. It can be seen that JavaScript calls the flex function, and flex needs to declare it, while flex does not need to call the JavaScript function.