Actionscript3 到 JavaScript 通信:最佳实践

发布于 2024-07-10 07:20:57 字数 355 浏览 5 评论 0原文

在更抽象的层面上,上一个问题,根据我的经验,有 3 种方法可以调用使用 AS3 嵌入 .swf 的 html 页面上的 javascript 函数:ExternalInterface、fscommand 和 navigatorToURL。

让我们比较和对比这些方法(也许还有我没有列出的其他方法),并讨论每种方法的优缺点 - 现在,ExternalInterface 似乎在灵活性方面是可行的方法,但实际上适合所有情况吗? 在执行速度或类似方面有具体的好处吗? 我很好奇——我们怎么想?

On a more abstract level then a previous question, in my experience there are 3 ways to call a javascript function on an html page from an embedded .swf using AS3: ExternalInterface, fscommand, and navigateToURL.

Let's compare and contrast these methods (and maybe others I haven't listed) and talk about the pros and cons of each - right now, ExternalInterface seems like the way to go in terms of flexibility, but is it right for all situations? Are there concrete benefits in terms of execution speed or anything like that? I'm curious - what do we think?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

染年凉城似染瑾 2024-07-17 07:20:57

创建ExternalInferface是为了使JS和Flash之间的通信更容易,因此使用其他任何东西都没有任何意义。 常见的做法是在调用某些 JS 之前先评估ExternalInterface.available 属性的值来检查其是否可用。 此属性告诉您要从中调用某些 JS 的 SWF 是否位于提供外部接口的容器内。 换句话说,如果使用ExternalInterface就可以了。 如果它不可用,则只需使用 flash.net.sendToUrl。 切勿使用 fscommand(),因为它使用 VBScript,可能会导致与页面上的其他 VBScript 发生冲突。 此外,您只能使用 fscommand 发送一个参数字符串,并且必须在 JS 端将其拆分。

ExternalInferface was created to make communication between JS and Flash easier, so it doens't really make sense to use anything else. Common practice is to check if its available first by evaluating the value of the ExternalInterface.available property before making a call to some JS. This property tells you if the SWF in which you want to call some JS from is inside a container that offers an external interface. In otherwords, if using ExternalInterface will work. If its not available then just use flash.net.sendToUrl. Never use fscommand() as it uses VBScript and can cause conflicts with other VBScript on a page. Additionally, you can only send one argument string with fscommand and have to split it on the JS side.

给妤﹃绝世温柔 2024-07-17 07:20:57

这完全取决于您是否希望通信同步,因为 ExternaInterface 可以返回数据,而 navigatoToURLfscommand 是异步的,并且只能调用 JavaScript 函数; 它们无法返回值或响应。

来自与外部接口相关的实时文档:

通过 ActionScript,您可以在 HTML 页面上执行以下操作:

  • 调用任意 JavaScript 函数。
  • 传递任意数量、任意名称的参数。
  • 传递各种数据类型(布尔值、数字、字符串等)。
  • 接收来自 JavaScript 函数的返回值。

通过 HTML 页面上的 JavaScript,您可以:

  • 调用 ActionScript 函数。
  • 使用标准函数调用表示法传递参数。
  • 向 JavaScript 函数返回一个值。

flash.external.ExternalInterface 类是 flash.system.fscommand 类的直接替换。

因此,使用ExternalInterface是flash和Javascript函数之间的首选方法或通信,但如果调用仅仅是异步的,则可以使用flash.net.navigateToURL

It all depends on if you want the communication to be synchronous or not as ExternaInterface can return data as where navigatoToURL and fscommand are asynchronous and can only call a javascript function; they cannot return values or a response.

From live docs in relation to External Interface:

From ActionScript, you can do the following on the HTML page:

  • Call any JavaScript function.
  • Pass any number of arguments, with any names.
  • Pass various data types (Boolean, Number, String, and so on).
  • Receive a return value from the JavaScript function.

From JavaScript on the HTML page, you can:

  • Call an ActionScript function.
  • Pass arguments using standard function call notation.
  • Return a value to the JavaScript function.

The flash.external.ExternalInterface class is a direct replacement for the flash.system.fscommand class.

So using ExternalInterface is the preferred method or communication between flash and a Javascript function, though if the call is merely Asynchronous it is ok to use flash.net.navigateToURL.

紧拥背影 2024-07-17 07:20:57

ExternalInterface

  • 您可以从 JS-AS 和 AS-JS 调用中获取返回值 对
  • 您的参数进行编码(使用数组、对象等进行调用,无需对其进行编码)
  • 跨浏览器
  • 发送 HTML 或 JSON 时存在缺陷(特殊编码),它 内部中断

getURL

  • 只能调用JS,不能获取返回值,您需要对数据进行编码
  • 比已弃用的好,在 Flash 10 中它被删除了
  • 它确实被删除了,所以不要使用它;)

fscommand

  • 来吧,ExternalInterface 是解决方案(对于2008)。

ExternalInterface

  • You can get the return value from JS-AS and AS-JS calls
  • Encodes your arguments (call with arrays, objects, etc. No need to encode them)
  • Cross browser
  • Flawed when you send HTML or JSON (special encoding), it breaks internally

getURL

  • You can only call JS, you not get the return value and you need to encode your data
  • Was nice than deprecated and in Flash 10 it's removed
  • It is really removed, so don't use it ;)

fscommand

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