Slick Little JavaScript Function to Parse GET Variables in URL

I was messing around this morning trying to parse the HTTP GET variables in JavaScript so that my web app could "see" the arguments passed into it, and act on them accordingly. This was something that the users have asked for in the alerts from my web app - a link back to the web app so that they can easily "drill down" to the data to see what the alert was really all about.

So I did some Googling on this and I was very pleased to find something like this:

  /**
   * This function parses all the GET variables for the current page
   * and returns a map of the key/value pairs to the caller. This can
   * be used in some pages to get these GET variables for processing.
   */
  function parseGETVars() {
    var retval = [];
    var urlChunks = String(document.location).split('?');
    if (urlChunks[1]) {
      var pairs = urlChunks[1].split('&');
      for (var i = 0; i < pairs.length; ++i) {
        if (pairs[i]) {
          var parts = pairs[i].split('=');
          retval[parts[0]] = unescape(parts[1]);
        }
      }
    }
    return retval;
  }

with this, I can then simply parse the GET variables into a global (page) variable and then use it in the code to see what I need to see.

With this, I was able to pass into one of my pages, the default portfolio to show - as opposed to the first one in the available list. It's a pretty slick little thing. Very nice.