使用ExternalInterface从JavaScript调用ActionScript 3.0/Flash中的函数
即使确保将“allowScriptAccess”设置为始终,我也无法使其工作。我成功地将 Flash 影片放入浏览器中,并调用 ReceiveDataFromFlashMovie() 并打印“到达这里”,但根据 Internet Explorer 中的错误消息,GetFlashMovieObject() 似乎只返回 NULL。我错过了什么吗?
HTML 文件头:
<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;
}
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我了解您的问题,您唯一要做的就是更改
顺序
对象嵌入代码之后的 :)。它没有找到它(返回未定义),因为它尚未嵌入。
As far as I understand your problem, the only thing you have to do is changing the order :)
put
after the object embed code. It didn't find it (returned undefined), because it was not yet embedded.
如果您使用 swfObject 嵌入 Flash 影片,则可以使用 swfobject.getObjectById 来检测您的 swf 并调用您的方法。
如果您不使用 swfObject,只需在 JS 代码上复制并粘贴 getObjectById 方法:
并通过执行以下操作来调用它:
看看这个,我实际上在我的博客上编写了一个小示例: http://www.nelsond8.com/?p=515
If you are using swfObject to embed your flash movie you can use swfobject.getObjectById to detect your swf and call your methods.
If you are not using swfObject just copy and past the getObjectById method on your JS code:
And call it by doing:
Have a look on this, I actually code a small example on my blog: http://www.nelsond8.com/?p=515
您不允许加载/初始化 SWF。
在访问 SWF 内的任何内容之前,您需要确保它已准备好处理回调。
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.