views:

112

answers:

1

I've been using ExternalInterface up until now in the rare cases I needed to communicate with HTML.

  • Is FlashVars considered a better/worse or newer/older practice?
  • or when do you use one or the other? What's the logic that I should follow to help me decide which one to use?
+5  A: 

FlashVars is not better or worse. It's just different. It allows you to pass some data to your swf at load time. If all you need is to pass you swf some context initially, FlashVars is what you want.

Now, if you require more interaction between your swf and it's host (which in most cases is a web browser that can run Javascript), ExternalInterface is there to help.

If you want to pass data from the swf to the host, use ExternalInterface.call from the AS side and set a corresponding JS function on your html. If the communication goes the other way round, use ExternalInterface.addCallback from the AS side and have a JS function that invokes this callback on your html. Of course, you can use both call and addCallback if you need it. Most of the times, though, I only use ExternalInterface.call, since I use this mechanism mostly for calling google analytics or some other tracking system.

An important aspect of ExternalInterface is that for it to succeed, both your swf and the JS code that handles the communication have to be loaded. This is obvious if you think about it for a second, but could lead to problems if you don't account for it. That is, if from JS you call a function that hasn't yet been added as a callback in your swf, well... it won't be called. The same is true the other way round, though it's more rare in practice.

Juan Pablo Califano
+1 that answer taught me something, thanks. Good question too. I use blazeDS mostly and haven't yet needed to learn how to connect with JavaScript. Now I've learned enough to make it work.
gmale