Advertisements

Archive

Posts Tagged ‘onload’

Reading Querystring using Jscript

January 24, 2012 1 comment

Hi,

Below is the Jscript function to parse query string (i.e., In to Parameter & Value pair)

// Read querystring and returns Parameter & Value pair Array

function fetchQueryStringParams() {

var qrStr = window.location.search;

var spQrStr = qrStr.substring(1);

var arrQrStr = new Array();

// splits each of pair

var arr = spQrStr.split(‘&’);

for (var i = 0; i < arr.length; i++) {

// splits each of field-value pair

var index = arr[i].indexOf(‘=’);

var key = arr[i].substring(0, index);

var val = arr[i].substring(index + 1);

// saves each of field-value pair in an array variable

arrQrStr[key] = val;

}

return arrQrStr;

}

How do I call this method?

function onload() {

var arrQrStr = fetchQueryStringParams();

alert(“First Name” + arrQrStr[‘fname’]);

alert(“Last Name” + arrQrStr[‘lname’]);

}

Hope it helps 🙂

Advertisements