How to invoke JavaScript code with Flex

Source: Internet
Author: User
Tags html page

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:

    1. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.