0

I have a query string in javascript which looks like this : Default.aspx?7KCN0008-001 and when i try to insert 7KCN0008-001 into the textbox i get [object Object] how can i put the value inside the textbox here is what i have:

function test() {
    var urlParams;
    (window.onpopstate = function () {
        var match,
            pl = /\+/g,  // Regex for replacing addition symbol with a space
            search = /([^&=]+)=?([^&]*)/g,
            decode = function (s) { return decodeURIComponent(s.replace(pl,"")); },
            query = window.location.search.substring(1);

        urlParams = {};
        while (match = search.exec(query))
            urlParams[decode(match[1])] = decode(match[2]);
    })();
 $('#txtStockCode').val(urlParams[decode(match[1])]);
}
0

2 Answers 2

1

I might be barking up the wrong tree, but as you wrote in your question, if URL is something like Default.aspx?7KCN0008-001, you could get the string 7KCN0008-001 by this code.

 query = window.location.search.substring(1);

So I guss you could simply put this string into the textbox

$('#txtStockCode').val(query);

The document of location.search is here:
http://www.w3schools.com/jsref/prop_loc_search.asp

Hope this helps.


EDIT:

Okay, if the query is 7KCN0008-001 SAVSS0.85B 2180 916941-000%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20 917418-060, I guess you could do something like this:

HTML:

<input type="text" id="t1">
<input type="text" id="t2">
<input type="text" id="t3">
<input type="text" id="t4">
<input type="text" id="t5">
<input type="text" id="t6">

Javascript:

var query = window.location.search.substring(1); // "7KCN0008-001 SAVSS0.85B ...."
var decoded = decodeURI(query);
var ary = decoded.replace(/\s{2,}/g, ' ').split(' '); 
for (var i = 0; i < ary.length; i++) { 
  $("#t"+(i+1)).val(ary[i]);
}

DEMO JSfidle is here:http://jsfiddle.net/naokiota/pH4mB/2/

Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

one more thing if i have URL like this: Default.aspx?7KCN0008-001 SAVSS0.85B 2180 916941-000%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20 917418-060,how can i put 7KCN0008-001,SAVSS0.85B,2180,916941-000,917418-060 on its own textboxes like 6 textboxes?
@billah77, okay. You mean there are six values separated by space? (It seems five values.)
my bad there are five values
0

I think you are confuse about the data structure. You first define it to be an Object and then you say its an array.

urlParams = {};

urlParams[decode(match[1])] = decode(match[2]);

You need to find out the value of decode(match[1]) and use this in the .val method as its an array....

$('#txtStockCode').val(urlParams[decode(match[1])]);

2 Comments

Okay since am really confused how can i change the structure of the data
:-) No structure of the data - the data structure. Array vs Object

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.