Saturday, 20 April 2013

Get query string values using javascript.


Get query string values using javascript.


You can use the pure JavaScript to read query string paramemeter value,

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

if following is an url.

http://www.test.com/test.html?name=bridzetech

then you can following javascript method to read name value in query string.

function readname()
{
      var nameval = getParameterByName('name');
alert(nameval);
}

that's it.