[thelist] JavaScript and GET name value pairs

Hardacker, Andrew Andrew.Hardacker at Compuware.com
Wed Dec 18 12:47:01 CST 2002


> Tom, this should work for you. Note that you have to grab location.search,
> not location.href
>
> There is at least one line that is customized for my own application -
> this.getValue returns false if there is no match for the key supplied. You
> can change it to, for example, an empty string if your requirements
> differ.
>
> Andy Hardacker
>
> /====================================================================
> //  PARSING THE LOCATION.SEARCH STRING
> //====================================================================
> // the constructor takes the location.search string, builds a bunch of
> arrays
> // and attaches some functions to the object
> function SearchData(search) {
>   this.keyValuePairs = new Array();
>   if (search.length > 1) {
>     this.search = search.substring(1, search.length); // remove leading
> "?"
>     this.keyValuePairs = this.search.split("&");
>   } else {
>     this.search = null;
>   }
>   // returns an array of the key=value pairs
>   this.getKeyValuePairs = function() { return this.keyValuePairs; }
>   // returns the value for a specified key
>   this.getValue = function(key) {
>     for(var j=this.keyValuePairs.length; j-- > 0;) {
>       if(this.keyValuePairs[j].split("=")[0] == key)
>       return this.keyValuePairs[j].split("=")[1];
>     }
>     return false;
>   }
>   // returns an array of the key names
>   this.getParameters = function() {
>     var a = new Array(this.getLength());
>     for(var j=this.keyValuePairs.length; j-- > 0;) {
>       a[j] = this.keyValuePairs[j].split("=")[0];
>     }
>     return a;
>   }
>   // returns a count of key/value pairs
>   this.getLength = function() { return this.keyValuePairs.length; }
> }
> /* usage example:
>   var test = new SearchData("?name=snitch&color=tabby&volume=loud");
>   // var test = window.location.search;
>   alert("parms = "+test.getParameters());
>   alert("color = "+(test.getValue("color") ? test.getValue("color") : "No
> color was defined."));
> */
>
>
>
The contents of this e-mail are intended for the named addressee only. It
contains information that may be confidential. Unless you are the named
addressee or an authorized designee, you may not copy or use it, or disclose
it to anyone else. If you received it in error please notify us immediately
and then destroy it.




More information about the thelist mailing list