JavaScript and Flash Communication

Source: Internet
Author: User
Tags function definition html page

When the flash is placed in the HTML container, you will often encounter the communication problems of AS and JS, such as: JS can call variables in as, methods, as can call JS variables, methods and so on. The answer is yes. With the continuous development of technology, the solution is also diverse.

In my summary of the HTML and flash between the "static" value of the article mentioned JS using the SetVariable method to set the variables in Flash,Kinglong that this method is obsolete. I agree with this, but above all the emphasis is not on the discussion of JS and as communication, so in addition to the AS and JS communication to do a personal summary, welcome to discuss.

To achieve the communication between JS and as, there are many alternative methods, but the early methods in the use of convenient and functional is not very perfect, the following to do a brief description:

First, GetURL

GetURL ("Javascript:history.go (-1)");


to access JavaScript in the page through the URL protocol, the code that controls the history of the browser is familiar, and so on. We usually use this method when we are customizing the page collection and sending mail. Although you can also call the page's own definition of JS function, but I personally think that the limitations are relatively large, the function of the parameter transfer is not very flexible, no return value, and can only implement as Call JS, and vice versa.

Two, fscommand commands

using Fscommand to invoke the as definition is also a common way, but we need to define a specified format in an HTML page myflash_ Dofscommand function, first define this function I personally feel trouble, but also can only implement as Call JS, no function return value.

Three, setvariable

The above two methods can only implement as Call JS, and setvariable on the contrary, as long as we do a little bit of processing, he can help us invoke the method in the as in disguise. The general idea is as follows: As in the set a state variable, and using the object's Watch method to monitor it, JS through the setvariable to modify the state variable, once detected the variable changes, it can be based on different state values to choose to perform the corresponding function in as. If you need to consider the user's lower version of the player, then you can consider the method, the individual think or more flexible.

can see that these practices have some limitations, so there are many times in our application that we have to use them in combination. And the following I would like to specifically introduce the practice of Externalinterface , through which you can easily implement the as and JS bidirectional method calls , and thus solve the two-way variable access, For a detailed description of the Flash Help documentation and the Adobe official Tutorials , here are two simple examples to illustrate the use of externalinterface.

One, as calling JS method (Instance demo)
Flash Code:

[
Import flash.external.*;
Get_btn.onrelease = function () {
Call the JS function in the page
[Color=red]var temp_str = String (Externalinterface.call ("Say", "Hello,world")); [/color]
Result_txt.text = Temp_str;
}
HTML code: Function say (TXT) {
return txt;
}


Yes, it is so simple, the JS function definition does not have any requirements, as in the use of the call method to invoke directly on it.

Second, JS call as Method
Flash in code:

[//Import Package
Import flash.external.*;
Provides JS access to the function name
var _method:string = "Say";
Specifies the scope of this variable in the local function, which can be set to null blank
var e_area:object = null;
As internal function name
var method:function = say;
Register a function with a list of containers
[Color=red]var Wassuccessful:boolean = Externalinterface.addcallback (_method, E_area, method); [/color]
To see if the registration was successful
if ([Color=red]wassuccessful[/color]) {
Result_txt.text = "Function registration success";
}
A local function
function Say (txt:string) {
Result_txt.text = txt;
}

 
<div>
<form>
<input type= "button" onclick= "Callexternalinterface ()" value= "JS calls as method"/>
</form>
<script>
Function Callexternalinterface () {
[Color=red]thismovie ("Demo"). Say ("Hello,world"); /color]
}
//browser-compatible Access DOM
function Thismovie (moviename) {
if (navigator.appName.indexOf ("Microsof T ")!=-1 {
return window[moviename]
}
Else {
return document[moviename]
}
}
</script>
</div>


where the red code is the core code, the principle is that the as end through the Addcallback function of the as internal definition of the method to call, allow the customization of another method name for JS to call this method, the function call successfully return TRUE, failure to return flase, In this example, the wassuccessful variable is used to determine whether the function is registered successfully. After the function registration is successful, JS can access the SWF object through the DOM, and then call the predefined method directly.

By comparison, you can see that using Externalinterface to communicate as and JS, the code can be simpler, clearer, and more powerful , but there are some details you need to understand, you need to use more than 8.0 players, for the calling JS function can not use recursion, while the security domain restrictions must also be considered in the .

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.