Obtaining query parameters in Javascript

The location object suffers from poor design in Javascript. Here’s a function to retrieve the query parameters from the page (if any).

The location object suffers from poor design in Javascript. Here’s a function to retrieve the query parameters from the page (if any).

function getQuery(){
  var s = location.search;
  var params=Array();
  if (s.length > 0) {
    s = s.split('?');
    s = s[1];
    s = s.split('&');
    for (i=0; i<s.length; i++) {
      params.push(
        s[i].split('=')
      );
    }
  }
  return params;
}

Enjoy, and if you find anything to add or remove, don’t hesitate to let me know in the comments!

Publié le 04 octobre 2007 et étiqueté : Technique.

Commentaires

Goulven, le 04 octobre 2007

Once again, this entry was hard to get through the door, because of escaping less than  entities… I had to edit it from the database!

Super Chinois, le 05 novembre 2007

This is what I use :

http://adamv.com/dev/javascript/fil...