GET Variables with Flash MX 2004

While working on my latest project at Provo Labs, I needed to use something like GET Variables with Flash MX 2004, so that I could pass parameters into the Flash application from the html page that it sat on. Here’s how to do it:

First, in the object tag on your html page, put your parameters on a url-encoded query string like so:

mp3Player.swf?fileName=1.mp3&volume=50

Make sure you place the full url with the querystring in both the embed tag and the param movie=”” tag as Internet Explorer uses the param tag and Firefox uses the embed tag to determine the source of your SWF.

Now for the ActionScript that does the magic:

var selfURL:String = new String(_root._url);
var queryString:String = new String();
var splitAt:Number = selfURL.lastIndexOf(“?”)+1;
var qsLength:Number = selfURL.length – splitAt;
queryString = selfURL.substr(splitAt,qsLength);

// parse the querystring into usable variables
var parameters:LoadVars = new LoadVars();
parameters.decode(queryString);

Your parameters variable will now act as the $_GET variable in php, so you can get your parameters by referencing parameters[‘id’] and so forth.

2 thoughts on “GET Variables with Flash MX 2004”

  1. ok..thankx 4 the tips…iam just a flas beginner..and im having trouble using variables on my flash

    thx 4 the tips

Comments are closed.