从 Flash 调用之前确保 JavaScript 对象存在

发布于 2025-01-04 12:50:16 字数 929 浏览 4 评论 0原文

我正在为一个我无法直接访问的网站开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深白境迁sunset 2025-01-11 12:50:17

一旦 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 the ExternalInterface.addCallback method to bind your function in ActionScript to another one in your JavaScript code.

Here's the documentation of that feature.

り繁华旳梦境 2025-01-11 12:50:16

为了鲁棒性,您需要检查该函数是否也存在:

var ok:Boolean = ExternalInterface.call(function() {
    if (typeof(customObject) === 'object' && typeof(customObject.doSomething) === 'function') {
        return true;
    }
}

ExternalInterface.call 是同步的,因此您应该发现它将等到该位完成后再继续下一个。

For robustness, you need to check whether the function is there too:

var ok:Boolean = ExternalInterface.call(function() {
    if (typeof(customObject) === 'object' && typeof(customObject.doSomething) === 'function') {
        return true;
    }
}

ExternalInterface.call is synchronous, so you should find that it will wait until this bit is finished until moving on to the next.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文