tomhoppe.com

Racing, Web Development, Photography, and Beer...Stuff that matters.

Friday, September 5, 2008

Get Parameter from Querystring via Javascript (getParam)

A while back I posted a method to grab all the querystrings out of a URL and parse them into arrays. While that is very useful sometimes, its overkill for other situations, when you have 1 or 2 variables in the querystring and you know their names already. For those times, I like to use a function "getParam". It takes in the variable name and returns you its value from the querystring.

function getParam(name) {
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var tmpURL = window.location.href;
  var results = regex.exec( tmpURL );
  if( results == null )
    return "";
  else
    return results[1];
}

Labels: ,

0 Comments:

Post a Comment

<< Home