Calling ActionScript functions from JavaScript using the ExternalInterface API in ActionScript 2.0

by Peter deHaan on April 3, 2008

in ExternalInterface

The following example shows how you can call an ActionScript function from your HTML template using JavaScript and the ExternalInterface class in ActionScript 2.0.

Full code after the jump.

  1. Copy and paste the following code into frame 1 of your Flash document:
// ActionScript 2.0
import flash.external.ExternalInterface;
 
stop();
 
ExternalInterface.addCallback("sayWhat", null, sayWhat);
 
function sayWhat():Void {
    gotoAndStop("success");
}
  1. Using the Text tool in Flash, add the text “1″ to the first frame.
  2. Create a blank keyframe on frame 10, select the new frame and add the text “2″.
  3. Copy and paste the following code into frame 10 of your Flash document:
stop();
  1. Save your document as “main.fla” and select File > Publish to generate the SWF, HTML, and JavaScript files.
  2. Select File > Publish Settings from the main menu and deselect the HTML check box from the Formats tab. This will prevent the HTML template from being overwritten if you republish your FLA for any reason.
  3. Using a text editor such as Dreamweaver, Notepad, TextEdit, etc, open the main.html file which was automatically created by Flash in step 5.
  4. Add the following code between the <head> and </head> tags:
<script language="javascript">
<!--
function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    } else {
        return document[movieName];
    }
}
 
function doWhat(value) {
    thisMovie(value).sayWhat();
}
-->
</script>
  1. After the </noscript> tag, add the following code:
<form>
<input type="button"
        value="Frame 2"
        onClick="doWhat('main');" />
</form>
  1. Finally, save your HTML file and open it in your favorite web browser. Clicking the Frame 2 button in the HTML file should now cause the SWF file to skip to the second state in your Flash application.
  2. Celebrate.

{ 5 comments… read them below or add one }

1 Usama Ahmed 12.11.08 at 3:20 am

Which flash player have you used inside the following example. I am trying to do it for flash player 6 and AS 2. But it is not working.

2 Peter deHaan 12.11.08 at 8:15 am

Usama Ahmed.

This particular example would have probably been written using Flash Player 9/ActionScript 2.0/Flash CS3. Newer examples are mainly written/tested against Flash Player 10/ActionScript 3.0/Flash CS4.

Only the latest and greatest here at ActionScriptExamples.com! ;)

Peter

3 Morgan 01.13.09 at 11:52 am

Yeah, this doesn’t work and mucking around hasn’t made it work either. gotoAndPlay(“success”); should be a frame label, no?

4 Esam Melad 10.06.09 at 9:07 am

Please Help me.
I made a coloring book in Flash. I finished everything, just remaining one thing. After the user finish painting the outline image must be save the final painting but I can’t find Action Script 2.0 code to save a move clip in user’s computer.
Note:
I don’t want to use any sever technology, it’s locally.

Please help me don’t forget me.

Thanks
Esam Melad

5 oskar 01.13.10 at 6:51 pm

Hi,

How can I call a function inside another class? I’ve tried using:
ExternalInterface.addCallback(“myFunction”, this, myClass.myFunction);

Thanks,
Oskar

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Previous post:

Next post: