Javascript calls functions in SWF

Source: Internet
Author: User

The externalinterface API
Previously, we used flash to call JavaScript functions. This method is generally used:
Geturl ("javascript: Alert ('Hello ');"); // note that the string is enclosed in double quotation marks with single quotation marks
However, using this method to call JavaScript Functions from flash still has many difficulties. Especially when dealing with multiple JavaScript function calls. The externalinterface API can be called seamlessly with JavaScript, and its capabilities can be used in the Flash timeline.
Before we officially start using it, we need to ensure that it is available in flash time. We need to import its class to the following:
Import flash. External .*;
Now, the first thing we can do is call JavaScript Functions.
Call Method
The call method is a static method for the externalinterface object. That is to say, we do not need to use an instance of the externalinterface object to call the method. We can call the call method directly through the object class, like this:
Externalinterface. Call (functionname: String, parameters );
This method has two parameters:
· Functionname-the name of the JavaScript function to be called must be in the string format.
· Parameters-parameters to be passed to the JavaScript function. They are separated by commas (,) and are optional.
These are the basic content. Now let's look at an example:
1. Create a flash file named external1.fla.
2. Create an as layer on the two layers, and name the layer below as "content"
3. in the content layer, create a text field, set it to the input text field, open the display border, and give it the name alert_txt, place it on the left of the scenario.
4. still on the Content layer, drag a button component in the scenario and place it on the right of the dynamic text, set its label to "alert" and name it alert_butn.
5. Select the as layer above and enter code on the first frame,
6. // call alert
7. Code:
Import flash. External .*;
Alert_butn.clickhandler = function (){
Externalinterface. Call ("alert", alert_txt.text );
The code above is to import the externalinterface class package. Set events for the button component, call the JavaScript function when you click it, and pass the text entered in the text to the JavaScript function through externalinterface.
Now release your video and place it and HTML on the server. When you test it on the server, whatever information you enter in the text, it will be displayed in the alert dialog box, however, if you test it locally, you may find that nothing happens when you click the button, which is caused by the security features of Flash Player.
Demo File
The above example is a method example. You can also use the geturl method. However, externalinterface can not only do this, but also send information to JavaScript, it can also receive information returned from JavaScript through the addcallback method.
Addcallback Method
The addcallback method allows JavaScript to call the flash time function. The basic syntax structure is as follows:
Externalinterface. addcallback (functionid: String, instance: object, functionname: function );
Similar to the call method, it is also a static method. It has three parameters.
· Functionid-the name that can be used to call the ActionScript function from JavaScript. This name does not need to match the actual name of the ActionScript method.
· Instance-this is the object to be parsed in this method. This object is not necessarily the object on which the method is located. You can specify any object (or null ).
· Functionname-The ActionScript method to be called from JavaScript.
Now let's start an example. In this example, we will use flash to call the Javascript command to ask the user to enter the color value. When the color value is entered, click OK, the method is as follows:
1. Create a flash file named external2.fla.
2. In the same way as in the first example, create a two-layer upper layer named as. The lower layer named "content ".
3. Drag a button component on the Content layer to the scene, place it in the upper left corner, and set the label to "color changing". The instance name is change_butn.
4. Make sure that the current content layer draws a box with a color value of 0x000000 and the size is 100*100. Of course, this is not limited.
5. convert this square to movieclip. The instance name is rec_mc.
6. Enter the following code on the first frame:
Import flash. External .*;
// Change the color of the square
Function onchange (CLR: Number ){
VaR temp_color: color = new color (rec_mc );
Temp_color.setrgb (CLR );
// Allow JavaScript to call the onchang Function
Externalinterface. addcallback ("onchange", this, onchange );
// Open the daily window
Change_butn.clickhandler = function (){
Externalinterface. Call ("callprompt", "What colors do you want to use? (Ex: 0xff0000 )");
Before the code, we need to first import the externalinterface class package, and then create a function that can change the color of blocks in the scenario, we call the addcallback method so that javascript can call the onchang function of flash. Finally, we call Javascript commands for button Creation events.
You need to publish SWF and HTML, and then use the editing software to open the editor. If you only call javascirpt from flash, you do not need to do anything. However, if you need to call the flash function using javascirpt, you still need to do some small work.
Add the following JavaScript code below the tilte Tag:

First, we create the variable me to store SWF references. Without it, we cannot call the flash function. Then we use the GETID function to set reference based on different browsers. Then we create the makecall function to call the flash function. Finally, we create the callpromt () function called by flash (). it will call the makecall function to send information back to flash.
Add onload to the attributes of the html body tag and set it to the GETID function, as shown below:

Note: When we set the function to the onload event, we pass the swf id to it. it can be found in the embedded position of SwF. Now, after page Initialization is complete, the GETID function will be called, and the variable me will be set so that javascript can call the flash function.
Upload the file to the server and perform a test. When you click the button and enter the color value in the prompt, the block color in flash will change.

Related Article

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: 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.