JS calls the method in flex and calls the JS sample to flex with reference and 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 SWF's Object


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.


//Call it by Method


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 establish a bridge between them, using
Externalinterface.addcallback ("Showparams", Showparams) when Flex initializes; 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.

But there is a problem that if Flex does not load successfully on the page, it will report an error that cannot 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.

Therefore, in the initialization of flex, add the
//Call initswf method
Externalinterface.call ("initswf") to load the page;
you are done.

There is a need to note:
in the Flex call JS method, this method can only be written in the SWF file, but I introduced in the JSP HTML, so can only be in the HTML JS, is called the parent page of the JS method.
The reference to the SWF file in the JSP, 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.