When flash is placed in an HTML container, it often encounters communication problems between AS and JS, such as whether JS can call variables and methods in as, and whether as can call variables and methods in Js. The answer is yes. With the continuous development of technology, there are also a variety of solutions.
In my summary of "static" value transfer between HTML and flash, I mentioned that JS uses the setvariable method to set variables in flash. kinglong thinks this method is outdated. I agree to this, but the above focus is not on the communication between JS and as. Therefore, we also make a personal summary of As and JS communication. You are welcome to discuss it.
Currently, many methods are available for communication between JS and as. However, early methods are not perfect in ease of use and functionality. The following is a brief description:
1. geturl ("javascript: history. Go (-1 )");
You can use the URL protocol to access JavaScript on the page. The code that controls the browser's history is familiar. For example, this method is often used to customize the page favorites and send emails. Although you can also call JS functions defined on the page, I personally think that the limitations are still relatively large. The parameter transfer of functions is not very flexible and there is no return value, in addition, it can only implement as to call JS, but not vice versa.
Ii. fscommand command
Using fscommand to call the as-defined method is also a very common method, but we need to define a myflash_dofscommand function with the specified format on the HTML page, first, I personally think this function is difficult to define, and I can only implement as to call JS without returning function values.
Iii. setvariable
The above two methods can only implement as to call JS, while setvariable is just the opposite. As long as we handle it a little bit, he can help us call the methods in the as in disguise. The general idea is as follows: Set a state variable in AS and monitor it using the watch method of the object. js uses setvariable to modify this state variable. Once the variable changes are detected, then you can choose to execute the corresponding function in as according to different status values. If you need to consider a player of a lower version, you can consider this method. I personally think it is more flexible.
We can see that these practices have some limitations, so we have to use them in combination in many cases. Next, I will introduce the externalinterface method. Through this method, you can easily implement bidirectional method calls between AS and JS, which also solves bidirectional variable access, for details, see the flash help document and Adobe official tutorials. The following two simple examples are provided to illustrate the use of externalinterface.
1. As method for calling js (Example demonstration)
Code in flash:
// Import the package
Import flash. External .*;
Get_btn.onrelease = function (){
// Call JS functions on the page
VaR temp_str = string (externalinterface. Call ("say", "Hello, world "));
Result_txt.text = temp_str;
}
Code in HTML: function say (txt ){
Return txt;
}
That's right. It's that simple. js function definition does not have any requirements. You can use the call method in as to call it directly.
Ii. js calls the as method (example)
Code in Flash: // import package
Import flash. External .*;
// Provide the function name for JS access
VaR _ method: String = "say ";
// Specify the scope of this variable in the local function. It can be set to null and left empty.
VaR e_area: Object = NULL;
// As internal function name
VaR method: function = say;
// Register the function to the container list
VaR wassuccessful: Boolean = externalinterface. addcallback (_ method, e_area, method );
// Check whether the registration is successful
If (wassuccessful ){
Result_txt.text = "function registration successful ";
}
// Local Function
Function say (txt: string ){
Result_txt.text = txt;
}
Code in HTML: <div>
<Form>
<Input type = "button" onclick = "callexternalinterface ()" value = "JS call as method"/>
</Form>
<SCRIPT>
Function callexternalinterface (){
Thismovie ("Demo"). Say ("Hello, world ");
}
// The browser is compatible with Dom
Function thismovie (moviename ){
If (navigator. appname. indexof ("Microsoft ")! =-1 ){
Return window [moviename]
}
Else {
Return document [moviename]
}
}
</SCRIPT>
</Div>
The red code is the core code, and its function is to register the internally defined methods of as called from the container through the addcallback function, you can customize another method name for js to call this method. If the function is successfully called, true is returned. If the function fails, flase is returned. In this example, the wassuccessful variable is used to determine whether the function is successfully registered. After the function is successfully registered, JS can access the SWF object through DOM, and then directly call the predefined method.
Through comparison, we can see that the code can be simpler, clearer, and more powerful when externalinterface is used for communication between AS and JS. However, you need to know some details, A player of more than 8.0 is required, and the called JS functions cannot be recursive. security domain restrictions must also be considered.
Article Source: http://www.elong8.net/blog/trackback.asp? Tbid = 425 & Action = addtb & tbkey = 9522c5eb9e87173e9c6540d21d3ef7350ca37b08