How to call flash actionscript callback method from javascript?

久未见 提交于 2019-11-28 01:20:02

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();
    }
    

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();

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.

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();

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!