Actionscript3 to JavaScript communication: best practices

此生再无相见时 提交于 2019-12-21 15:19:32

问题


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?


回答1:


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.




回答2:


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.




回答3:


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).


来源:https://stackoverflow.com/questions/312880/actionscript3-to-javascript-communication-best-practices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!