javascript - Variable Value Is Printing '[ object Object]' -
i using script below take value url , display value within input field.
var querystring = function () { // function anonymous, executed , // return value assigned querystring! var query_string = {}; var query = window.location.search.substring(1); var vars = query.split("&"); (var i=0;i<vars.length;i++) { var pair = vars[i].split("="); // if first entry name if (typeof query_string[pair[0]] === "undefined") { query_string[pair[0]] = decodeuricomponent(pair[1]); // if second entry name } else if (typeof query_string[pair[0]] === "string") { var arr = [ query_string[pair[0]],decodeuricomponent(pair[1]) ]; query_string[pair[0]] = arr; // if third or later entry name } else { query_string[pair[0]].push(decodeuricomponent(pair[1])); } } return query_string; }(); document.addeventlistener("domcontentloaded", function() { document.getelementbyid("field34").value = querystring; }, false);
for reason when variable querystring
prints screen displays within input field:
[object object]
however when console.log variable this:
console.log(querystring) undefined object { value: "yx57wdl" }
which has value in print screen.
any idea why might happening?
thanks, nick
your variable querystring
assigned object. when assign object attribute of dom element, object cast string.
in javascript, whenever object cast string, .tostring()
method called. default returns [object object]
.
if want string representation object can either override .tostring()
method or avoid implicit casting constructing string hand, e.g., json.stringify()
.
Comments
Post a Comment