Search code examples
javascriptflashactionscriptexternalinterface

Calling a function in ActionScript 3.0/Flash from JavaScript using ExternalInterface


I can't get this to work even after making sure to set "allowScriptAccess" to always. I successfully put the flash movie in the browser and call ReceiveDataFromFlashMovie() and print "Got here" but it seems like GetFlashMovieObject() only returns NULL according to an error message in Internet Explorer. Am I missing something?

Head of the HTML file:

<script type="text/javascript">
function getFlashMovieObject(movieName)
{
  if (navigator.appName.indexOf("Microsoft") != -1) { 
    return window[movieName] 
  } 
  else { return document[movieName] }
}

function ReceiveDataFromFlashMovie()
{
     document.write("Got here");
     var callResult = getFlashMovieObject("MakingButtons").myFunction();
     return callResult;
}

</script>

HTML:

<script type="text/javascript">
document.write("Hello World.")
ReceiveDataFromFlashMovie();
document.write(callResult)
</script>

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="500" height="500" id="MakingButtons" align="middle">
<param name="allowScriptAccess" value="always" />
<param name="movie" value="MakingButtons.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<embed src="MakingButtons.swf" quality="high" bgcolor="#ffffff" width="550" height="400" name="MakingButtons" align="middle" allowScriptAccess="always" swlliveconnect="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>

ActionScript 3.0:

import flash.text.TextField;
import flash.external.*;
// The name of the Flash variable to be called in JavaScript
var flashFunction:String = "myFunction";
var instance:Object = null;

// Callback function executed by the name of variable
var realFunction:Function = callMe;
ExternalInterface.addCallback(flashFunction, realFunction);

var foo = "Goodbye!";
function callMe():String
{
    return foo;
}

Thank you!


Solution

  • As far as I understand your problem, the only thing you have to do is changing the order :)

    put

    <script type="text/javascript">
    document.write("Hello World.")
    ReceiveDataFromFlashMovie();
    document.write(callResult)
    </script>
    

    after the object embed code. It didn't find it (returned undefined), because it was not yet embedded.