Tuesday, July 14, 2009

How To Parse Url Query String Using JavaScript

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

How To call = alert(parseUrl(QryStringParameterName));


Example Click Here

1 comment:

Unknown said...

This is the code I wrote you can use it also;

function RequestQueryString(textToSplit, queryName) {
var queryValue = null;
textToSplit = textToSplit.toString().toLocaleLowerCase();
queryName = queryName.toString().toLowerCase();
queryName = queryName + "=";
var result = textToSplit.toString().replace(/\?/g, "&");
var regex = new RegExp("&");
result = result.split("&");
if (result.length < 1) {
return null;
}
for (var i = 0; i < result.length; i++) {
var isCorrectString = result[i].toString().toLowerCase();
if (isCorrectString.search(queryName) != -1) {
queryValue = isCorrectString.split("=")[1];
break;
}
}
return queryValue;
}