如何从 javascript 调用 flash actionscript 回调方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7657842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call flash actionscript callback method from javascript?
提问by Tricks By Sam
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]:
我试图从 JavaScript 调用 flash 回调方法。但它似乎不起作用。flash动作脚本示例代码如下[简化]:
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]:
javascript示例代码写在[简化]下面:
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".
它总是在 fire bug 控制台中显示错误“flashFile.js_method_to_call is not a function”。
回答by Vladimir Tsvetkov
Here's something that should work really good:
这里有一些应该非常有效的东西:
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);
Use this for the HTML:
<body> <div id="flashDiv"></div> </body>
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]; } }
Call the Flash method:
function callFlashMethod() { thisMovie("${swf}").js_method_to_call(); }
使用 SWFObject.js 嵌入 Flash 内容:
// 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);
将此用于 HTML:
<body> <div id="flashDiv"></div> </body>
要调用您的 Flash 方法,请使用以下模式:
// Functions needed for calling Flex ExternalInterface function thisMovie(movieName) { if (navigator.appName.indexOf("Microsoft") != -1) { return window[movieName]; } else { return document[movieName]; } }
调用 Flash 方法:
function callFlashMethod() { thisMovie("${swf}").js_method_to_call(); }
回答by Yogev Shelly
You get a reference to your embedded SWF object and use it to make a call to your as3 method.
您获得对嵌入的 SWF 对象的引用,并使用它来调用您的 as3 方法。
//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();
回答by Pierre de LESPINAY
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
这里有一个有趣且非常详细的教程 http://bytes.com/topic/flash/answers/694359-how-do-i-access-flash-function-using-javascript#post2759970
回答by ymutlu
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.
此页面很好地描述了解决方案,只需尝试使该示例工作即可。这样你就可以理清问题了,Vladimir Tsvetkov 的回答就完成了。
回答by jhocking
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();
另外,我猜这只是在这里粘贴时的一个错字,但flashFile.js_method_to_call;
应该是flashFile.js_method_to_call();