SoFunction
Updated on 2025-04-11

Flash and JS communication method

Today I will mainly learn how js and flash communicate.

1. Learn first
getURL(url, window, variables)

Use the getURL statement to enable the specified browser window to display the specified URL address. Parameter (url) specifies the URL address of the WEB page document to be displayed. The parameter (window) specifies the browser window to display the WEB page document. It can be a window name specified as a custom one or

If you want it to execute the execution of the js function, then you can do this
on (release) {
  getURL(\"JavaScript:alert('hello,world')\");

}
If you call a custom function
on (release) {
  getURL(\"JavaScript:aiqi('test')\");

}
Then the html page needs to define this function, for example
function aiqi(s){
alert(s);
}

This allows simple flash and js communication

So how do you let js control flash?
SetVariable() function
(\"js\").SetVariable(\"myvar\", \"asdfasdfasdf\");  

js is the id of the swf file in html

How to use javascript to call function in flash.
The direct method should be Miyou.
You can simulate it. Set a variable in flash, javascript control changes the value of this variable, and flash detects this change
Change the magnitude value and change the execution function;
flash     
 --------------   
 var   stat=0;   
 setInterval(test,300){   
         if   (stat==1)   doFlash();   
 }   
 function   doFlash(){   

 }   

 javascript   
 --------------------   
 (\"stat\",1); 

In this example, I think the interaction between js and flash has been implemented.
Let me explain it. Actually very simple.
First define a function in the html file
  function aiqi(s){
// Assign value to flash
  ("stat",s);  
  }  
Note js is the id of swf file
There are two buttons in the SWF file. The ASs are
on (release) {
  getURL("javascript:aiqi(1)");
}
on (release) {
  getURL("javascript:aiqi(0)");
}
Through these two buttons, the parameters are passed to the aiqi() function of js,
aiqi() function dynamically changes the stat median value in flash.
In swf AS is
 var stat=0;
 var c=0;
 setInterval(function(){
         if   (stat==1)  
      doFlash();   
 },1000)  
 function   doFlash(){   
         var1=c++;   
 }   
When flash detects a change in stat value, call doFlash().
// [Action in Frame 1]
function doFlash()
{
    var1 = c++;
} // End of the function
var stat = 0;
var c = 0;
setInterval(function ()
{
    if (stat == 1)
    {
        doFlash();
    } // end if
}, 1000);
on (release)
{
    getURL("javascript:aiqi(1)");
}

on (release)
{
    getURL("javascript:aiqi(0)");
}