/    Sign up×
Community /Pin to ProfileBookmark

passing object between pages

Hi,

I want pass a javascript object from 1 page to another using POST.
when i do this, on the 2nd page i get [object Object]. I want to be able to access the properties of this object. How do i do this?

page1:
var testObj = new Object();

testObj.Name = “harry”;
testObj.Age = “19”;
testObj.Location = “bombay”;

$(‘#tbtestbox’).val(testObj );
document.forms[“formtest”].submit();

<form id=”formtest” action=”page2.aspx” method=”post”>
<input id=”tbtestbox” type=”hidden” name=”tbtestbox” />

</form>

page 2:

var testObj2 = new Object();
testObj2 = ‘<%=Request.Form[“tbChartData”] %>’;

The obove testObj2 get the value [object Object]. How do i access the 3 properties that i had created above?

Thanks in advance.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@MarPloJan 31.2012 — Hi,

Try, to create and pass the object in JSON format.
var testObj = {"name":"harry", "age":19, "location":"bombay"};
You can find about JSON on the net.
Copy linkTweet thisAlerts:
@mjyJan 31.2012 — I believe that when you assign testObj to the value property of the <input> control
<i>
</i>$('#tbtestbox').val(testObj );

the object is converted to text by an implicit call to its toText() method. The default toText() method of an object usually just returns "[object Object]". You could define a custom toText() method in your object that would return a string that represents the entire content of the object in JSON format, as MarPlo suggested. Otherwise, your Page 1 code can remain as it is. Then, of course, you'll have to read the JSON formatted string in your Page 2.

BTW, with ECMAScript5, the JSON object provides a set of properties for working with the JSON format, although I'm not sure how well supported they are by current browsers.
Copy linkTweet thisAlerts:
@pokakauthorFeb 03.2012 — Thanks MarPlo,

Since i had a complex type object which contained properties, arrays, and arrays of objects...I used the JSON.stringify() method of the json2.js script file to convert my complex type to string.

Then to reconvert the string to its original type, the JSON.parse() method from the same json2.js file.

This makes it very easy to move around complex types between pages.
Copy linkTweet thisAlerts:
@mjyFeb 04.2012 — The JSON object is part of ECMAScript 5. For the sake of older browsers, you might want to test whether this object is present, and if it isn't use the public-domain json2.js module available at https://github.com/douglascrockford/JSON-js/blob/master/json2.js. json2.js is apparently almost identical to the ECMAScript 5 JSON object.
×

Success!

Help @pokak spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...