Mike T. Henderson

I make interactive things.

Categories:


More Mike:

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 Comments | Posted under Strings Bookmark and Share

9 Comments »

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

    Comment by Manfred — 6/9/08 @ 11:08 pm

  2. Very nice! Thanks!

    Comment by Mike T. Henderson — 6/10/08 @ 3:00 am

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

    Comment by Blackhatseo — 7/26/08 @ 10:01 am

  4. Nice post, you got some good points there – thank you.

    Comment by Import from China — 7/28/08 @ 4:45 am

  5. 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!

    Comment by Bevin Lorenzo — 8/8/08 @ 11:11 pm

  6. Files can be downloaded at: http://mikethenderson.com/downloads/as3/_external/getQuery_v1.zip

    Comment by Mike T. Henderson — 8/11/08 @ 1:00 pm

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

    Comment by barry — 8/11/08 @ 5:06 pm

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

    Comment by Work at homes moms — 8/25/08 @ 4:04 pm

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

    Pingback by Mike t. Henderson » AS3 Return Query String Value — Version 2 — 2/2/09 @ 7:17 pm

Leave a comment