Flex calls JavaScript code
In Flex development, JS code needs to be called sometimes, such as closing the browser, or opening a prompt box in the browser under some circumstances. In general, Flex uses the call () function of the ExternalInterface class to call JS Code. The call () function parameter is a String-Type JS Code segment.
You can call JavaScript on the Html page. By interacting with JavaScript, you can change the Style and call remote methods. You can also pass the data to the Html page and return it to Flex after processing. There are two main methods to accomplish this function: ExternalInterface () and navigateToUrl ().
The simplest way to call JavaScript in Flex is to use ExternalInterface (). You can use this API to call arbitrary JavaScript, PASS Parameters, and obtain the returned value. If the call fails, Flex throws an exception. ExternalInterface encapsulates browser-supported checks and can be viewed using the available attribute. ExternalInterface is easy to use. The syntax is as follows:
- Flash. external. ExternalInterface. call (function_name: String [, arg1,...]): Object;
The function_name parameter is the name of the JavaScript function to be called, and the following parameter is required by JavaScript.
As code
ExternalInterface. call (FUNCTION_USEREXIT );
ExternalInterface. addCallback ("checkExit", checkExit );
PrivatestaticvarFUNCTION_USEREXIT: String = "document. insertScript = function ()" +"
{"+" Window. onbeforeunload = function () "+" {"+" varflexObj = MarineDataEdit. checkExit ();
"+" If (flexObj! = "") "+" {"+" ReturnflexObj; "+"} else {"+" return; "+ "}";
PublicfunctioncheckExit (): String {varuserExitStr: String = "if you leave now, all your information will be invalid! ";
ReturnuserExitStr;
ExternalInterface. call (FUNCTION_USEREXIT );
ExternalInterface. addCallback ("checkExit ",
CheckExit); privatestaticvarFUNCTION_USEREXIT: String = "document. insertScript = function ()" +"
{"+" Window. onbeforeunload = function () "+" {"+" varflexObj = MarineDataEdit. checkExit ();
"+" If (flexObj! = "") "+" {"+" ReturnflexObj; "+"} else {"+" return; "+ "}";
PublicfunctioncheckExit (): String {varuserExitStr: String = "if you leave now, all your information will be invalid! ";
ReturnuserExitStr;
The addCallback function of the ExternalInterface class is the return value of calling the.