SoFunction
Updated on 2025-04-02

JS calls methods in Flex and pass parameters to flex and call js example

First, there is a swf file, and there is also an automatically generated html file.
Then, in another file, through an iframe, it is introduced, that is, swf is introduced.

Now that I want to pass parameters to flex in, and call the method in flex, the method I use is:
First, write a method to call flex in jsp, as follows
Copy the codeThe code is as follows:

function initSWF(){
//Get swf's object
var obj = ["rightframe"].ReverseQuery; //rightframe is the name of the iframe, ReverseQuery is in, and the id of swf is introduced to object.
//Calling its method
var start = "<%=startPoint%>";
var end = "<%=endPoint%>";
(start,end);
}
In html, there are methods
/*Only call the js method of this page in flex*/
function initSWF(){
();
}
In flex, there are the following methods:
public function showParams(start:String,end:String):void{
(start);
(end);
}

At this time, it is necessary to establish a bridge to open communication between them. When initializing flex, use
("showParams",showParams); //The former is the method name it exposed to js, ​​and the latter is its method name in flex.
In this way, the call can theoretically be implemented. You can implement it by placing the initSWF method in the onload method of the page.

However, a problem arises. If flex is not loaded successfully in the page, an error that the object cannot be found will be reported. Therefore, we need to call the method in swf after ensuring that the swf file is loaded.
But listening to it loading is obviously a waste of emotion, so the strategy we adopted is to wait until it loads, and then call the initSWF method of js to open up all the links.

Therefore, when flex is initialized, add
//Calling the initSWF method of loading the page
("initSWF"); //Please place it after registering the external method.
The mission is done.

Something to note:
Calling the js method in flex, this method can only be written in the file that introduces swf, but I introduced html in jsp, so it can only be js in html, which is the js method of the parent page.
In jsp, the reference to the swf file should be used to ["rightframe"]