views:

2051

answers:

3

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?

+5  A: 

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.

Matt W
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?
matt lohkamp
Updated my answer a bit.
Matt W
nice, that makes sense.
matt lohkamp
+4  A: 

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

For the best tutorials check out Lee BrimeLow's gotoAndLearn.com and theflashblog.com.

My works are here pixeldev.net

Brian Hodge
the asynchronous point is a good one.
matt lohkamp
+2  A: 

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).
digitarald
well-put - the deprecation and subsequent removal in flash 10 cinches it for me.
matt lohkamp