Every so often we come across one of those 'gotchas' when working with JavaScript. One such 'gotcha' is reading query string variables from the URL. Oddly enough JavaScript has no built in way to do this. Luckily it's easy enough for us to implement our own such function:

var request = {
  queryString: function(item){
    var value = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)","i"));
    return value ? value[1] : value;

To use this function, simply do:


This will return the contents of the query string variable variableName or null if it doesn't exist. That's all there is to it! Thanks for reading!