Welcome to weblogs.com.pk Sign in | Join | Help

Adnan Siddiqi

Lets get technical


  • View Adnan Siddiqi's profile on LinkedIn
extracting query String values in Javascript

To fetch query String variables in server side languages is pretty common but it's not common to fetch queryString values in javascript. For instance you have a URL like http//site.com/file.html?v=1&t=3 , i order to process this request you don't have any back end language support like php,jsp etc so you have to write some parsing mechanism to deal with stuff starts after question mark(?).

Recently I faced a situation where I had to pass values across HTML pages, after googling a bit  I found solution by using query string in HTML files. Luckily I found a beautiful code snippet which I am sharing with you guys. Use it since it works pretty cool.


function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[ i ].split("=");
    if (pair[0] == variable) {
      return pair[1];
  alert('Query Variable ' + variable + ' not found');
and then you can thing like for Url http://site.com/myfile.html?fn=Adnan&ln=Siddiqi&cty=Karachi
alert( getQueryVariable("fn") ); //Adnan
alert( getQueryVariable("ln") ); //Siddiqi
alert( getQueryVariable("cy") );  //Karachi



Cool, isn't it? THe thing needs to be noted is window.location.search method which returns things after ?



Posted: Monday, March 03, 2008 12:05 AM by kadnan
Filed under: , ,


No Comments

Anonymous comments are disabled