7

After redirecting to home.html page, i can see the querystring values which i had given in the previous page.

Home.html?FirstName=dd&LastName=ee&smtButton=Submit

And am getting the result as:

firstname = undefined
lastname = undefined
age = undefined

Could anyone help me to solve this?

JS:

function getParams() {
    var idx = document.URL.indexOf('?');
    var params = new Array();
    if (idx != -1) {
        var pairs = document.URL.substring(idx + 1, document.URL.length).split('&');
        for (var i = 0; i < pairs.length; i++) {
            nameVal = pairs[i].split('=');
            params[nameVal[0]] = nameVal[1];
        }
    }
    return params;
}

params = getParams();
firstname = unescape(params["firstname"]);
lastname = unescape(params["lastname"]);
age = unescape(params["age"]);
document.write("firstname = " + firstname + "<br>");
document.write("lastname = " + lastname + "<br>");
document.write("age = " + age + "<br>");
dfsq
  • 191,768
  • 25
  • 236
  • 258
Venil Aravazhi
  • 289
  • 3
  • 7
  • 16

2 Answers2

10

Inside your function function getParams() you are declared variable var params = new Array(); , I think this makes confusion for you

if a match is found , you assigned url param like params[nameVal[0]] = nameVal[1];, this actually not adding value into array object. so params.length is 0 .but it will works as array is instance of object .. ie params instanceof Object is true

so change into basic object .. to avoid confusion

function getParams() {
    var idx = document.URL.indexOf('?');
    var params = {}; // simple js object
    .. here goes other code
}

and object key is case sensitive, so FirstName will work ..

firstname = unescape(params["FirstName"]);

to print all values try this

params = getParams();

for( var i in params ){
    console.log( i , params[i] );
}

it will print

FirstName dd    
LastName ee    
smtButton Submit

and I have modified your getParams code

function getParams() {

    var params = {},
        pairs = document.URL.split('?')
               .pop()
               .split('&');

    for (var i = 0, p; i < pairs.length; i++) {
           p = pairs[i].split('=');
           params[ p[0] ] =  p[1];
    }     

    return params;
}
rab
  • 4,134
  • 1
  • 29
  • 42
  • Hence am getting the same result. I've to simply display those values in this page – Venil Aravazhi Mar 27 '13 at 09:13
  • @VenilAravazhi did tried `firstname = unescape(params["FirstName"]);` , it case sensitive – rab Mar 27 '13 at 09:17
  • Instead of using console.log( i , params[i] );, use document.write(i," = "+ params[i] + "
    ");. Now its working properly. Your code is very helpfull to me. Thank you very much.
    – Venil Aravazhi Mar 27 '13 at 09:31
0

You can convert a querystring from the location.search-string to a js-object using:

String.prototype.q2obj = function(){
    var qArr = this.split('&')
        ,qObj = {}
        ,i =-1;
    while(++i<qArr.length) {
            qfrag = qArr[i].split('=');
            qObj[qfrag[0]] = qfrag[1];
   }
   return qObj;
};
//usage
var queryObj = location.search.substr(1).q2obj();
KooiInc
  • 119,216
  • 31
  • 141
  • 177