Obtaining query parameters in Javascript

Publié le 4 octobre 2007

Tags : Technique

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!

2 commentaires

Goulven le 4 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 5 novembre 2007

This is what I use :

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

Page précédente Page précédente Page suivante Page suivante