javascript function: getQuerystringValue(sQueryName, bCaseSensitive)

This function will return the value of a given querystring param. It takes two parameters:

  1. string sQueryName: The param name to look for in querystring
  2. boolean bCaseSensitive: Should the param name match be case sensitive? [false]

Please note it will only return the value of the last parameter, if you have a querystring parameter which is repeated.

function getQuerystringValue(sQueryName, bCaseSensitive) {
  /// <summary>Returns the value of a given querystring param name. If param is there more than once, the last version will be choosen. Example: getQuerystringValue('pageid'); If param name not in querystring null will be returned.</summary>
  /// <returns type="String|null" />
  /// <param name="sQueryName" type="String">The param name to look for in querystring</param>
  /// <param name="bCaseSensitive" type="Boolean">Should the param name match be case sensitive? [false]</param>
  var QueryValue;
  var bCaseSensitive = (arguments.length==2) ? arguments[1] : false;
  var sLoc ='';
  var i = (bCaseSensitive) ? sLoc.lastIndexOf(sQueryName) : sLoc.toLowerCase().lastIndexOf(sQueryName.toLowerCase());
  if (i>-1) {
      var selectedParam = sLoc.substr(i, sLoc.length-i).split('&');
      if (selectedParam.length>0) {
        QueryValue = unescape(selectedParam[0].split('=')[1]);
  return QueryValue

The function includes Microsoft XML Comments, which will give it intellisence when used in Visual Studio 2010.



Fixed bug: I discovered a bug when using case bCaseSensitive=false (default).


Leave a Reply