Actionscript3 to JavaScript communication: best practices

亡梦爱人 提交于 2019-12-04 06:47:19
Matt W

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.

Brian Hodge

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.

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