8

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>");
3
  • 1
    can we see your code ? Commented Mar 27, 2013 at 7:19
  • @VenilAravazhi: please edit add that code to your question. Commented Mar 27, 2013 at 7:21
  • possible duplicate of How to get the query string by javascript? Commented Mar 27, 2013 at 7:22

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hence am getting the same result. I've to simply display those values in this page
@VenilAravazhi did tried firstname = unescape(params["FirstName"]); , it case sensitive
Instead of using console.log( i , params[i] );, use document.write(i," = "+ params[i] + "<br>");. Now its working properly. Your code is very helpfull to me. Thank you very much.
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();

2 Comments

How shall i print the values which are in the object?
Any way you like it. E.g. in your case queryObj.FirstName would contain the value of FirstName

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.