How to pass javascript object from one page to other

Few ways

  1. Server side postback

    Have a POST form on your page and save your serialized object inside a hidden input then post it to the other page. You will be able to process that data on the server and most likely put it back somehow into the page. Either as javascript object or anything else.

  2. Client side URL examination

    Make a GET request to your other page by attaching your serialized object to URL as:

    http://www.app.com/otherpage.xyz?MyObject=SerializedData

    That other page can then easily parse its URL and deserialize data using Javascript.

  3. What’s in a window.name = local cross-page session

    This is a special technique that’s also used in a special javascript library that exposes window.name as a dictionary, so you can save many different objects into it and use it as local cross-page-session. It has some size limitations that may affect you, but check the linked page for that and test your browsers.

  4. HTML5 local storage

    HTML5 has the ability of local storage that you can use exactly for these purposes. But using it heavily depends on your browser requirements. Modern browsers support it though and data can be restored even after restarting browsers or computers…

  5. Cookies

    You can always use cookies but you may run into their limitations. These days cookies are not the best choice even though they have the ability to preserve data even longer than current window session.

Javascript object serialization

You will of course have to use some sort of a (de)serializer on your client side in some of the upper cases. Deserializers are rather easy to find (jQuery already includes a great function $.getJSON()) and are most likely part of your current javascript library already (not to even mention eval()).

But for object to JSON string serialization I’d recommend json2.js library that’s also recommended by John Resig. This library uses in-browser implemented JSON (de)serialization features if they exist or uses it’s own implementation when they don’t. Hence recommendation.

Leave a Comment