Flex interacts with JavaScript, mainly relying on the flex externalinterface, which provides addcallback and call methods.
The following example demonstrates how Flex calls JavaScript and JavaScript calls flex.
JS Code
-
- FunctionHello (PARAM ){
-
- Alert (PARAM );
-
- Return
"JSP Hello :"+ Param;
-
- }
-
-
- FunctionCallflexfunction (){
- VaRX = Document. getelementbyid ("Flexobject"). Flexhelloworld ("Hello",
"World");
-
- Alert (X );
-
- }
Flex code
-
- <?XML
Version="1.0"
Encoding="UTF-8"?>
- <MX: Application
Xmlns: MX=Http://www.adobe.com/2006/mxml"
Layout="Absolute"
-
- Initialize="Initapp ()">
-
-
- <MX: script>
-
- <! [CDATA
- Import MX. Controls. Alert;
-
-
- Public Function flexhelloworld (param1: String, param2: string): String {
-
- Alert. Show ("param1:" + param1 + "; param2:" + param2 );
-
- Return "hello" + param1 + param2;
-
- }
-
- Public Function initapp (): void {
-
- Externalinterface. addcallback ("flexhelloworld", flexhelloworld );
-
- }
-
-
- Public Function jsphello (): void {
- VaRS: String=
Externalinterface. Call ("hello", "flex ");
-
- Alert. Show (s );
-
- }
-
-
- ]>
- </MX: script>
-
-
- <MX: button
X="52"
Y="58"
Label="Call JavaScript"
Click="Jsphello ()"/>
-
- mx: application