How to Use Flex to call JavaScript code

Source: Internet
Author: User

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:

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

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.