String.protoype.toQueryParams

Maninder, Singh
Thu, 27 Oct 2005 06:46:14 -0000

Hi, I am trying to play with all the methods in the Array Object.

Just wanted to check with you guys if this is the correct explanation of
toQueryParams().
If yes, then I''ll add it to the documentation as well.

Works for me (but I am not sure if it''s the correct way to use it).

  /* String.prototype.toQueryParams takes a query string, i.e, string with
     name value pairs separated with ''&'', splits them up
and returns an object
     using whose properties we can get the values of the params */
  /* Usage:
		var queryString =
"?name=mandy&email=mandiv-/E1597aS9LQAvxtiuMwx3w@public.gmane.org&age=20";
		name = queryString.toQueryParams().name;
		email= queryString.toQueryParams().email;
		age  = queryString.toQueryParams().age;
		alert(name+" "+email+" "+age);
  */
  toQueryParams: function() {
    var pairs = this.match(/^\??(.*)$/)[1].split(''&'');
    return pairs.inject({}, function(params, pairString) {
      var pair = pairString.split(''='');
      params[pair[0]] = pair[1];
      return params;
    });
  },

Thank you,
Mandy.