从 Flash 调用之前确保 JavaScript 对象存在
我正在为一个我无法直接访问的网站开发一个 Flash 应用程序。 Flash 应用程序应该调用网站上由网站发布者定义的 JavaScript 函数。有人建议我在从 ActionScript 调用其函数之前检查 javascript 对象是否存在:
var ok:Boolean = ExternalInterface.call(function() {
return typeof customObject !== \'undefined\'
}
如果我继续:
if (ExternalInterface.available && ok) {
ExternalInterface.call('customObject.doSomething', someStr);
}
此 if 的条件是否始终为 false,因为保存到 ok
中的调用有在我使用检查之前可能尚未完成,或者 ExternalInterface.call
是即时的吗?换句话说,在确定是否可以安全地假设 customObject 的存在之前,我是否必须等待第一次调用的结果。
编辑:按照评论中的建议更新代码:
if (ExternalInterface.available) {
var ok:Boolean = ExternalInterface.call('function() { return typeof customObject !== \'undefined\' }');
if (ok) {
ExternalInterface.call('customObject.doSomething', someStr);
} else {
.. do some fallback
}
} else {
.. do some fallback
}
I am developing a flash application for a website I have no direct access to. The flash application is supposed to call a javascript function on the website, defined by the website publisher. I got advised to check for the existance of the javascript object before calling its' function from actionscript:
var ok:Boolean = ExternalInterface.call(function() {
return typeof customObject !== \'undefined\'
}
If I then continue with:
if (ExternalInterface.available && ok) {
ExternalInterface.call('customObject.doSomething', someStr);
}
Will this if's condition always be false, because the call that gets saved into ok
has possibly not finished before I use the check, or is the ExternalInterface.call
instantenious? In other words, would I somehow have to wait for the result of the first call before determining if I can savely assume the existance of customObject.
Edit: Updated code as suggested in comments:
if (ExternalInterface.available) {
var ok:Boolean = ExternalInterface.call('function() { return typeof customObject !== \'undefined\' }');
if (ok) {
ExternalInterface.call('customObject.doSomething', someStr);
} else {
.. do some fallback
}
} else {
.. do some fallback
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一旦 swf 文件加载,
ExternalInterface.call
方法就可用(而此时您的 JavaScript 文件或变量可能不存在)。因此,您想要做的就是使用ExternalInterface.addCallback
方法将 ActionScript 中的函数绑定到 JavaScript 代码中的另一个函数。这是该功能的文档。
The
ExternalInterface.call
method will be available as soon as the swf file has loaded (whereas your JavaScript file or variable might not be there at that moment). So what you wanna do is use theExternalInterface.addCallback
method to bind your function in ActionScript to another one in your JavaScript code.Here's the documentation of that feature.
为了鲁棒性,您需要检查该函数是否也存在:
ExternalInterface.call 是同步的,因此您应该发现它将等到该位完成后再继续下一个。
For robustness, you need to check whether the function is there too:
ExternalInterface.call is synchronous, so you should find that it will wait until this bit is finished until moving on to the next.