访问通过JavaScript ActionScript函数 [英] Accessing an ActionScript function via Javascript
问题描述
我试图调用使用 ExternalInterface.addCallback
API中的动作脚本功能,但我似乎无法得到它的工作。这是我有:
I'm trying to call a function in an action script using the ExternalInterface.addCallback
API, but I can't seem to get it to work. Here's what I have:
动作:
//MyClass.as
package {
import flash.display.Sprite;
import flash.external.ExternalInterface;
public class MyClass extends Sprite
{
public function MyClass()
{
ExternalInterface.addCallback('getStringJS', getStringAS);
}
public function getStringAS():String
{
return "Hello World!";
}
}
}
注:我使用的是柔性mxmlc编译如果该事项本编译成SWF 的
HTML / JavaScript的:
HTML/Javascript:
<!doctype html>
<html>
<head>
<title>User Identification</title>
<head>
<body>
<object id="MyClass" name="MyClass" type="application/x-shockwave-flash" data="MyClass.swf" width="1" height="1">
<param name="movie" value="MyClass.swf">
<embed src="MyClass.swf" width="1" height="1">
</object>
<script type="text/javascript">
var flash = document.getElementById("MyClass");
var str = flash.getStringJS();
alert(str);
</script>
</body>
</html>
我收到的错误是:
The error I'm getting is:
Uncaught TypeError: Object #<HTMLObjectElement> has no method 'getStringJS'
我也试过在的情况下SWF文件是不加载超时增加,但我没有用这种方法无论是任何成功。
I also tried adding in a timeout in case the swf file wasn't loading, but I didn't have any success with that method either.
有什么想法?
干杯,
迈克
推荐答案
我想它了。处理的关键信号通过 ExternalInterface.call
的javascipt的,所以我们知道为确保SWF被加载。最通用的方式来做到这一点如下:
I figured it out. The key way to signal the javascipt through ExternalInterface.call
so we know for sure that the swf is loaded. The most "Universal" way to do this is as follows:
MyClass.as
MyClass.as
//MyClass.as
package {
import flash.display.Sprite;
import flash.external.ExternalInterface;
public class MyClass extends Sprite
{
public function MyClass()
{
ExternalInterface.addCallback('getStringJS', getStringAS);
if (ExternalInterface.available) {
ExternalInterface.call("isConnectedFlex");
}
}
public function getStringAS():String
{
return "Hello World!";
}
}
}
index.html的
index.html
<!doctype html>
<html>
<head>
<title>User Identification</title>
<head>
<body>
<object id="MyClass" name="MyClass" type="application/x-shockwave-flash" data="MyClass.swf" width="1" height="1">
<param name="movie" value="MyClass.swf">
<embed src="MyClass.swf" width="1" height="1">
</object>
<script type="text/javascript">
var flash = document.getElementById("MyClass");
function isConnectedFlex() {
var str = flash.getStringJS();
alert(str);
}
</script>
</body>
</html>
这篇关于访问通过JavaScript ActionScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!