www.webdeveloper.com
Results 1 to 5 of 5

Thread: passing object between pages

Hybrid View

  1. #1
    Join Date
    Jan 2012
    Posts
    2

    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.

  2. #2
    Join Date
    Feb 2011
    Posts
    231
    Hi,
    Try, to create and pass the object in JSON format.
    Code:
    var testObj = {"name":"harry", "age":19, "location":"bombay"};
    You can find about JSON on the net.

  3. #3
    Join Date
    Jan 2012
    Location
    United States
    Posts
    15
    I believe that when you assign testObj to the value property of the <input> control
    Code:
    $('#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.

  4. #4
    Join Date
    Jan 2012
    Posts
    2
    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.

  5. #5
    Join Date
    Jan 2012
    Location
    United States
    Posts
    15
    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/...aster/json2.js. json2.js is apparently almost identical to the ECMAScript 5 JSON object.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles