Development – Leet 4 Life

Software Development Code Samples and More

Skip to: Content | Sidebar | Footer

Access Query String values with JavaScript

19 December, 2012 | JavaScript | By: Michael Adams

I was looking for a way to access query string values with javascript. I stumbled across a couple of articles that pointed me in the right direction, but unfortunately I forgot to bookmark them. I will try to find them again and provide a link to provide them the proper credit. In the meantime here is the implementation:

var urlParams = {};
   (function () {
      var e,
      a = /\+/g, // Regex for replacing addition symbol with a space
      r = /([^&=]+)=?([^&]*)/g,
      d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
      q =;
      while (e = r.exec(q))
         urlParams[d(e[1])] = d(e[2]);

// You can reference the querystring value like so...
var variablename = urlParams["QUERYSTRINGNAME"];

This code parses through the URI components and uses a regular expression to pull out the name value pairs that are contained within and then returns it as a collection of the query strings and their values. This is one of the simplest implementations I have seen to retrieve query string values out in JavaScript.

The Original Code can be viewed Here

Write a comment