5

I tried to call a flash callback method from JavaScript. But it seems not working. The flash action script example code is like below [Simplified]:

import flash.events.ActivityEvent; 
import flash.events.StatusEvent; 
import flash.external.ExternalInterface;

var test_var = ExternalInterface.addCallback("js_method_to_call", flash_method);


function flash_method()
{   
  return "test"; 
}

The javascript example code is written below [Simplified]:

 function callFlashMethod(){
   var flashFile = eval("window.document.test");
   flashFile.js_method_to_call;
 }
 function loadTest(){
   swfobject.embedSWF("test.swf", "test", "1", "1", "10.0.0", false);
 }

 $(document).ready(function(){
   loadTest();
   callFlashMethod();
 });

It is always display the error in fire bug console "flashFile.js_method_to_call is not a function".

Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307
Tricks By Sam
  • 438
  • 1
  • 4
  • 11

5 Answers5

14

Here's something that should work really good:

  1. Use SWFObject.js for embedding the Flash content:

    // Embedding through SWFObject rocks in comparison with Adobe shits:
    var flashvars = {};
    
    var params                  =   {};
    params.menu                 =   "false";
    params.salign               =   "t";
    params.scale                =   "noscale";
    params.wmode                =   "transparent";
    params.allowScriptAccess    =   "always";
    
    var attributes              =   {};
    attributes.id = "${swf}";
    
    swfobject.embedSWF("${swf}.swf", "flashDiv", "${width}", "${height}", "9.0.0", "", flashvars, params, attributes);
    
  2. Use this for the HTML:

    <body>
        <div id="flashDiv"></div>
    </body>
    
  3. To call your Flash method use this pattern:

    // Functions needed for calling Flex ExternalInterface
    function thisMovie(movieName) 
    {
        if (navigator.appName.indexOf("Microsoft") != -1) 
        {
            return window[movieName];
        } 
        else 
        {
            return document[movieName];
        }
    }
    
  4. Call the Flash method:

    function callFlashMethod()
    {
        thisMovie("${swf}").js_method_to_call();
    }
    
Vladimir Tsvetkov
  • 2,983
  • 3
  • 26
  • 36
  • I am getting the error in firebug console `thisMovie("test").js_method_to_call is not a function` – Tricks By Sam Oct 05 '11 at 07:37
  • 1
    Are you using the JS from my sample for loading the SWF? Make sure that `attributes.id = "test";` The placeholder variable `${swf}` will work only if you build with the Flash Builder and if this code is placed inside the index.template.html and you have annotated appropriately the entry point Sprite of your app. – Vladimir Tsvetkov Oct 05 '11 at 07:54
9

You get a reference to your embedded SWF object and use it to make a call to your as3 method.

//AS3 Code
ExternalInterface.addCallback("helloFromJS",helloFromJS);

private function helloFromJS():void
{
    trace("JS is saying hello");
}


//HTML Code
<object width="100%" height="100%" id="Test">
          <param name="movie" value="Test.swf"/>

//JS Code
var swfObject = document.getElementById("Test");
swfObject.helloFromJS();
Yogev Shelly
  • 1,373
  • 2
  • 13
  • 24
1

There is an interesting and quite detailed tutorial here http://bytes.com/topic/flash/answers/694359-how-do-i-access-flash-function-using-javascript#post2759970

Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307
1

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html?filter_flash=cs5&filter_flashplayer=10.2&filter_air=2.6

This page describe the solution very well, just try to make that sample work. So you can sort out the problem, and Vladimir Tsvetkov's answer is complete.

ymutlu
  • 6,585
  • 4
  • 35
  • 47
0

I'm not sure about this line:

var flashFile = eval("window.document.test");

I would use:

var flashFile = document.getElementById("test");

Also, I'm guessing this was just a typo when pasting here, but flashFile.js_method_to_call; should be flashFile.js_method_to_call();

jhocking
  • 5,527
  • 1
  • 24
  • 38
  • I have tried all 3 options below but seems not working. `document.getElementById("test")`, `$("#test")`, `eval("window.document.test")` – Tricks By Sam Oct 05 '11 at 07:26