ExternalInterface.addCallback("在js里可调用的flash方法名",flash内方法)//在flash中通过这个方法公开 在js中可调用的flash内的方法
ExternalInterface.call("js方法",传给js的参数)//在flash里调用js里的方法
使用flash.external.ExternalInterface
步骤一:flash中定义可以访问的函数hello
flash as文件中的关键代码:import flash.external.ExternalInterface
function hello(){
return "测试成功了哦~~"
}
//允许js调用flash中的函数 参数1:要调用flash函数的js函数,参数2:被调用flash函数
ExternalInterface.addCallback("helloas",hello)
步骤二:js中定义可以访问的函数hello和flash文件加载
<script type="text/javascript">function hello(v)
{ //js中定义的测试函数
alert(v)
}
</scritp>
<body>
//flash组件
<object id="test" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="" width="560" height="270"> </object>
</body>
步骤三:flash访问js中的hello方法
ExternalInterface.call("hello", "jacky")步骤四:js访问flash的hello方法
document.getElementById("test").helloas()应该是编码问题,可能是js生成的字符窜 编码 不是utf8或者gb2312的 你可以试试我写的这个转码方法:输入的参数str 要转码的字符窜第二个是转码类型 默认值是gb2312
function encode(str:*,method:String="gb2312"):String
{
var byteArray:ByteArray = new ByteArray
byteArray.writeBytes(str)
byteArray.position = 0
return byteArray.readMultiByte(byteArray.length,method)
}