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.TextF开发者_高级运维ield;
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!
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.
If you are using swfObject to embed your flash movie you can use swfobject.getObjectById to detect your swf and call your methods.
swfobject.getObjectById("MakingButtons").myExternalMethod();
If you are not using swfObject just copy and past the getObjectById method on your JS code:
function getObjectById(objectIdStr) {
var r = null;
var o = getElementById(objectIdStr);
if (o && o.nodeName == "OBJECT") {
if (typeof o.SetVariable != UNDEF) {
r = o;
}
else {
var n = o.getElementsByTagName(OBJECT)[0];
if (n) {
r = n;
}
}
}
return r;
}
And call it by doing:
getObjectById("MakingButtons").myExternalMethod();
Have a look on this, I actually code a small example on my blog: http://www.nelsond8.com/?p=515
You are not allowing for the SWF to load/initialize.
Before you can access anything inside the SWF you need to make sure its ready to handle callbacks.
精彩评论