Mike T. Henderson

Interactive Design & Art Direction

AS3 QueryString Class

May 9 2008

Today I was working on a video player in AS3 that I wanted to be able to play any video on initial load by passing a variable through the query string. I had done this before in AS2 using the ExternalInterface class but wasn't sure if it was still valid in AS3. After a little quick research, I found that this is one thing that they left alone between the two versions.

So after a few minor AS2 to AS3 migrating, I now have an AS3 version of my QueryString class. The following app allows you to search through a query string for a value of a specified variable. To test, add a query string to the url and play.

package  {

import flash.external.ExternalInterface;

public class QueryString {

private static var _match:Boolean;
private static var _value:String;

public function QueryString() {
// constructor
}

public static function getQueryString ():String {
// get full query string
var _query:String = ExternalInterface.call("document.location.search.toString");
return _query;
}

public static function getQueryValue ( qvar:String ):String {

// reset vars
_match = false;
_value = "";

// get full query string
var _query:String = ExternalInterface.call("document.location.search.toString");
// build array
var qarray = _query.split("");

for (var i:int = 0; i < qarray.length; i++) {
// build search term
if (qarray[i] != "?" && qarray[i] != "=" && qarray[i] != "&") {
_value += qarray[i];
}

// check to see if search term is a var or the last value in the search
if (qarray[i] == "?" || qarray[i] == "&" || i == qarray.length-1) {
if (_match == true) {
break;
}

_value = "";
}

// check to see if search term matches the defined var name
if (qarray[i] == "=") {
if (_value == qvar) {
_match = true;
}
_value = "";
}
}
if  (_match == false) {
_value = "";
}
return _value;
}
}
}

Class Description:
Used to read query string and query values from url

Public Static Methods:
getQueryString():String -- returns full query string from url
getQueryValue( qvar:String ):String -- returns value of var name from query string in url

Properties of getQueryValue():
qvar:String -- name of variable you wish to retrieve value from

Example:

import com.mikethenderson.external.QueryString;
// create text field
var myQString:TextField = new TextField();
var myQValue:TextField = new TextField();
myQString.y = 0;
myQValue.y = 35;
// add to display list
addChild( myQString );
addChild( myQValue );
// if query string = ?name=mike&age=27
myQString.text = QueryString.getQueryString()); // returns ?name=mike&age=27
myQValue.text = QueryString.getQueryValue("name"); // returns mike

9 Posted under: Strings

9 Responses

  1. Manfred says:

    Hi, there's also a Url.as class available here:
    http://manfred.dschini.org/2008/05/12/as3-url-class/
    Anyways, great stuff!

  2. Very nice! Thanks!

  3. Blackhatseo says:

    Added. Nice work on this one. Btw, my blog is dofollow, stop by and grab a link. Bompa

  4. Import from China says:

    Nice post, you got some good points there - thank you.

  5. Bevin Lorenzo says:

    Could you attach the shown SWF and related AS files as shown above as I am having difficulty recreating this. Thanks for the great post!

  6. barry says:

    Thanks a lot Mike, this is a very nice solution, cheers for posting it!

  7. I read a simliar post just the other day by Sandra Kosineck but yours is much better.

  8. [...] while back I created a little class, using the ExternalInterface.call method, to return a query string value from the url. This came in [...]

Leave a Reply