Archive for July, 2006

Best way to get address variables into Flash

Friday, July 21st, 2006

I've seen too many wrong or old soulotions for sending query string variables into Flash (page.html?var1=Jon&var2=Smith... etc.). In fact, if you'll google it you'll find this old Macromedia article from the year 2000 and about Flash 4, while most of it still applies it's definitely old. These days, we're using swfobject, if you don't use it then you should, swfobject comes with a "global" javascript function called getQueryParamValue and you can use it for setting query/address string variables into FlashVars as follows:

JavaScript:
  1. var so = new SWFObject("movie.swf", "flashMovie", "250", "150", "8", "#FFFFFF");
  2. so.addVariable("var1", getQueryParamValue("var1"));
  3. so.addVariable("var2", getQueryParamValue("var2"));

These variables will be availble as _root.var1, _root.var2, etc. For complete explanation go here. The best thing about FlashVars is that they are available for use as soon as the Flash movie starts, and before everything else, so you can refer, for example, to the variable _root.var1 in the first line of code, as opposed to another old method setVariable, where we had to wait for the variable availability using somthing like watch.

Though the advantage of FlashVars, personly, I don't like _root variables, it seems unclean, especially when it's a long query string that'll make tons of them floating in my _root. Since Flash 8 we can use ExternalInterface to call to getQueryParamValue and get an immediate and synchronous response from within Flash, that way we can have a neater control over owr address variables, for example:

Actionscript:
  1. var sVar1:Object = ExternalInterface.call("getQueryParamValue", "var1");

Play with the example, try different variables.

If you don't use swfobject, I guess you can easily write your own javascript method for getting address variables, but why bother, I don't think you can easily improve on that one. I also don't think that Geoff Stearns, the author of swfobject, will care if you'll copy this function:

JavaScript:
  1. function getQueryParamValue(param){
  2. var q = document.location.search || document.location.hash; if(q){
  3. var startIndex = q.indexOf(param +"=");
  4. var endIndex = (q.indexOf("&", startIndex)> -1) ? q.indexOf("&", startIndex) : q.length;
  5. if (q.length> 1 && startIndex> -1) {
  6. return q.substring(q.indexOf("=", startIndex)+1, endIndex);
  7. }
  8. }
  9. return "";
  10. }

It's great for non flash websites as well.

Get example files

Get swfobject

MSDN Flash

Wednesday, July 5th, 2006

If you go to download Microsoft Expression Interactive Designer beta (shorten that name plz !), you will probably stumble at the "MSDN Flash" banner. Actually the full banner text is "Register for MSDN Flash eNewsletter.

MSDN Flash

So, did MS also discovered the Flash platform, and she is so excited, that she's publishing an electronic-newsletter about it ? Or maybe it's an upcoming merger between MS and Adobe ?! Or maybe a world peace ?. After clicking on the banner it all become clear "All of Microsoft's Developer News in One E-mail". Oh, so you meant News-Flash, you funny little Microsoft ;), are you tring to fool us there, you crazy you.

IMHO this is no coincidence. I'm not sure what's MS want to achieve here, might be just to irritate Adobe or may be she wants that people that will search for "the new Flash" will find her Sparkle, hence also the url is - msdn.microsoft.com/flash. Well your Sparkle is very nice but it isn't Flash.