Actionscript3 to JavaScript communication: best practices
Asked Answered
B

3

6

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?

Bologna answered 23/11, 2008 at 20:1 Comment(0)
M
6

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.

Mesopotamia answered 23/11, 2008 at 20:1 Comment(1)
okay - I like it, that's what it was made for, the VBScript thing, the single argument thing, and the navigateToURL as an alternative - so, when you check for the availability of ExternalInterface, are you checking on the flash side, or is it somehow checking to see if javascript is there?Clemenceau
D
5

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.

Devote answered 23/11, 2008 at 20:1 Comment(0)
B
2

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).
Brief answered 23/11, 2008 at 20:1 Comment(1)
well-put - the deprecation and subsequent removal in flash 10 cinches it for me.Clemenceau

© 2022 - 2024 — McMap. All rights reserved.