Method call between JavaScript and flex

Source: Internet
Author: User

Call the flex method in javascript:

 

1. Call the flex method in Javascript
In Flex, you can use externalinterface to call the flex method by adding the specified public method to the callable method list of the Flex application. In a flex application, you can call addcallback () to add a method to this list. Addcallback registers an ActionScript method as a method that can be called by JavaScript and VBScript.
The addcallback () function is defined as follows:
Addcallback (function_name: String, closure: function): void
The function_name parameter is the method name called by the script on the HTML page. The closure parameter is the local method to be called. This parameter can be a method or an object instance.

For example:
<Mx: SCRIPT>
Import flash. External .*;
Public Function myfunc (): number {
Return 42;
}
Public Function initapp (): void {
Externalinterface. addcallback ("myflexfunction", myfunc );
}
</MX: SCRIPT>
On the HTML page, obtain the reference of the SWF object, that is, the ID attribute of the SWF declared with <object.../>, for example, myflexapp. Then you can use the following method to call the method in flex.
<Script language = 'javascript 'charset = 'utf-8'>
Function callapp (){
VaR x = myflexapp. myflexfunction ();
Alert (X );
}
</SCRIPT>
<Button onclick = "callapp ()"> call app </button>

 

 

 

Ii. Call Javascript in flex
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 ().
In Flex, the simplest way to call Javascript 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.
For example, how to call JavaScript Functions
Add the following method to the flex application:
<Mx: SCRIPT>
<? XML version = "1.0" encoding = "iso-8859-1"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml">
<Mx: SCRIPT>
Import flash. External .*;

Public Function callwrapper (): void {
VaR F: String = "changedocumenttitle ";
VaR M: String = externalinterface. Call (F, "New title ");
Trace (m );
}
</MX: SCRIPT>
<Mx: button label = "Change document title" Click = "callwrapper ()"/>
</MX: Application>
The following function definitions are available in the HTML page:
<Script language = "JavaScript">
Function changedocumenttitle (){
Optional parameter Doc ument. Title =;
Return "successful ";
}
</SCRIPT>

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.