Flex and JavaScript Interop

Source: Internet
Author: User

Flex will eventually generate the SWF file to deploy the Web page in an ActiveX manner (html,jsp,asp,aspx, etc.), because ActiveX is a standalone body it can not directly get the element data of the page, the same asp,aspx and other service-side code does not operate it, This makes the flex information interaction very isolated. It is important to exchange information about different pages in a Web user program, which usually makes it clear what the next page should do; If Flex doesn't get this information then flex will be compromised in real life! So to find information on the Internet, found that their fears are superfluous. Flex provides related objects with the flexibility to make flexible calls to and from the current Web page's JavaScript. That means you can invoke the internal methods of flex through JavaScript, and you can also call the relevant JavaScript methods on the page directly in Flex.

Flex provides a Externalinterface object for interoperability with JavaScript. objects provide the Addcallback and cell methods, respectively:

Addcallback method

Used to register an internal method to call JavaScript.

function callMe(name:String):String {
  return name+":" + txtUserName.text;
}

In fact, JavaScript cannot directly invoke Flex's internal methods, so how can JavaScript be invoked?

First, by Externalinterface.addcallback ("MyFunction", callMe), the method of registration, you can put the method in Applicaion. Initialize of the event.

The first argument to the method is that the external invocation name is the name of the method that JavaScript calls.

The second parameter of the method is: the agent performs the internal function of flex.

JavaScript is invoked in the following ways:

function Button1_onclick() {
alert(document.all('相关ActiveX控件名称').myFunction('bbq'));
}

Call method

Used to invoke the JavaScript method in the current page.

<script id="clientEventHandlersJS" language="javascript">
function getuserid()
{
   return 'henry';
}
</script>

In flex you can invoke the GetUserID () method in the following ways.

var id:string= externalinterface.call ("GetUserID");

Alert.show (ID);

Flex is doing a good job of integrating with JavaScript, and you don't have to work too much to make data exchange with page data elements, and you can also invoke existing AJAX features.

The next thing to learn is how the flex application instances of different pages share information.

Finally, there is an old saying, interested friends to learn more about flex.

The Flex SDK help file is very detailed and basically finds a way to use it.

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.