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:

reyou 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;
}

Creative Commons License
Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.