Actionscript3 到 JavaScript 通信:最佳实践
在更抽象的层面上,上一个问题,根据我的经验,有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建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.
这完全取决于您是否希望通信同步,因为
ExternaInterface
可以返回数据,而navigatoToURL
和fscommand
是异步的,并且只能调用 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 wherenavigatoToURL
andfscommand
are asynchronous and can only call a javascript function; they cannot return values or a response.From live docs in relation to External Interface:
The
flash.external.ExternalInterface
class is a direct replacement for theflash.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
.ExternalInterface
getURL
fscommand
ExternalInterface
getURL
fscommand