JS calls the method in flex and to the flex in the reference and Flex call JS Example _flex

Source: Internet
Author: User
First, there is a SWF file, test.swf, and there is also an automatically generated HTML file, test.html.
Then, in another file, test.jsp, through the IFRAME, the introduction of the test.html, that is, the introduction of SWF.

Now I want to pass the argument to flex in test.jsp, and call the methods in Flex, and I'm going to use the following method:
First, write a method to invoke flex in the JSP, as follows
Copy Code code as follows:

function initswf () {
Get the object of the SWF
var obj = window.frames["Rightframe"]. Reversequery; Rightframe is the name of the IFRAME, Reversequery is in test.html, introducing the ID of the SWF as object.
How to call it
var start = "<%=startPoint%>";
var end = "<%=endPoint%>";
Obj.showparams (Start,end);
}
In HTML, there are methods
/* In Flex can only call the JS method of this page * *
function initswf () {
Parent.initswf ();
}
In Flex, there are the following methods:
Public Function Showparams (start:string,end:string): void{
Alert.show (start);
Alert.show (end);
}

At this point, you need to create a bridge between them to open communication, in flex initialization, to use
Externalinterface.addcallback ("Showparams", showparams); The former is the name of the method it exposes to JS, and the latter is its method name in Flex.
In this way, it is theoretically possible to implement the call. The Initswf method can be implemented in the OnLoad method of the page.

However, a problem arises that if Flex does not load successfully on the page, it will report an error that does not find the object. Therefore, we need to make sure that the SWF file is loaded and then invoke the method in the SWF.
But listening to it loaded completed obviously a little wasteful feelings, so, we adopt the strategy is, and so it loaded, and then call JS initswf method, you can get through all the links.

So, in the case of flex initialization, join
Invoke the Initswf method of a loaded page
Externalinterface.call ("initswf"); It is to be placed after registering the external method.
Done.

There are areas to be noted:
In the flex Call JS method, this method can only write in the introduction SWF file, but I introduced the JSP in the HTML, so can only be in the HTML of JS, is called the parent page of the JS method.
In the JSP for the SWF file reference, with the help of window.frames["Rightframe"]

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.