Flex calls JavaScript code
In flex development, it is sometimes necessary to call the JS code, such as closing the browser or, in some cases, opening the browser prompt box, and so on. In general, Flex uses the call () function of the Externalinterface class to invoke the JS code, where the parameter of the called () function is the string JS code segment.
You can call JavaScript in an HTML page, and by interacting with JavaScript, you can change the style and invoke the remote method. You can also pass the data to the HTML page, processed and then returned to flex, there are two main ways to complete this function: Externalinterface () and Navigatetourl ().
The easiest way to invoke JavaScript in Flex is to use Externalinterface (), which can use this API to invoke arbitrary JavaScript, pass arguments, get return values, and if the call fails, Flex throws an exception. Externalinterface encapsulates a browser-supported check that can be viewed with the available property. The use of Externalinterface is very simple, and the syntax is as follows:
- Flash.external.ExternalInterface.call (Function_name:string[,arg1,...]): O Bject;
The parameter function_name is the function name of the JavaScript to invoke, followed by the parameters that JavaScript requires.
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 expire!" ";
RETURNUSEREXITSTR;
Externalinterface.call (Function_userexit);
Externalinterface.addcallback ("Checkexit",
Checkexit);p rivatestaticvarfunction_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 expire!" ";
RETURNUSEREXITSTR;
The Addcallback function of the Externalinterface class is to call the return value of the as end.