Access Querystring using Javascript

var qsParm = new Array();
function qs() {
 var query = window.location.search.substring(1);
 var parms = query.split(‘&’);
 for (var i=0; i<parms.length; i++) {
  var pos = parms[i].indexOf(‘=’);
  if (pos > 0) {
  var key = parms[i].substring(0,pos);
  var val = parms[i].substring(pos+1);
  qsParm[key] = val;
  }
 }
}example : if url is  www.yoursite.com/default.aspx?id=123&sessionid=swew2323od92891ks

Using the above code   alert(qsParm[‘id’] )  returns 123

2 thoughts on “Access Querystring using Javascript

  • March 25, 2008 at 12:39 pm
    Permalink

    Can use another split here:

    var ar = parms[i].split(“=”);
    if(ar.length == 2) {
    qsParm[ar[0]] = ar[1];
    }

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.