Are you new? Read the FAQ and catch up on!
2k views

Call a function in your Flash movie from Javascript

First, you need to define a callback in your Flash movie. To do it, use the addCallback function of the ExternalInterface class:

ActionScript 3
  1. // ...
  2.  
  3. ExternalInterface.addCallback("callbackName", flashMethod);
  4.  
  5. // ...
  6.  
  7. private function flashMethod(param1:int, param2:String):void
  8. {
  9.    // ...
  10. }
  11.  

­

After that, it is very easy to call your method using javascript:

Javascript
  1. document.getElementById("flashMovie").flashMethod(12345, "12345");
  2.  

­

IMPORTANT WARNING: The code above would work in an ideal world. Unfortunately, the different browsers treat flash embeds in different ways and you need to use some hacks to make it work in all of them. Read this great article and follow its instructions: REFERENCING FLASH MOVIE USING JAVASCRIPT

Tags: ActionScript 3.0 JavaScript flash ExternalInterface

Embed: