0

Does anybody if a solution exists or if it is even feasible to save or serialize all the Javascript objects of a webpage in their current state to a file and restore those objects in a new browser instance of the same webpage?

I have a webpage which contains various Javascript objects and would like a way to take a "snapshot" of the page and restore the page to the exact same state at a later time. I've been able to serialize and restore the page's entire DOM tree, but the Javascript objects are not restored so the page looks correct but does not function correctly.

treaint
  • 699
  • 1
  • 8
  • 13

3 Answers3

3

In addition to cookies as others have mentioned, you can try using HTML5's localStorage.

Also here is a storage abstraction library that will help you use all of them, and take care of cross-browser support:

https://github.com/marcuswestin/store.js

Chris Cherry
  • 28,118
  • 6
  • 68
  • 71
2

You should save the variables you want to an object, and encode it using JSON, then save it to a cookie. To restore it, just retrieve the object from the cookies and use a JSON parser.

  • I've actually thought about going that route, except using HTML5's localstorage instead of cookies. The Javascript objects that I'm working with are actually dojo widgets, and I haven't been able to find a way to serialize or encode them to JSON. So far every method I've tried runs into "too much recursion" problems since the objects contain functions and circular references. – treaint Feb 24 '11 at 18:51
  • JSON skips functions, and it don't work with circular references. Also, encode only the objects properties that should be saved, like x and y, and skip the ones you don't want. For my multiplayer engine (written in AS3), I always implement a function called getNetworkTable, which returns a new object which contains everything that should be sent to the client, you can use a similar logic for your problem. –  Feb 24 '11 at 20:04
  • By the way, for a more compact storage, I recommend: http://code.google.com/p/m28keyvalue, it's usage is just like JSON: `KeyValue.encode(obj, compress)` (remember to pass true to compress) and `KeyValue.decode(str)` –  Feb 24 '11 at 20:04
0

You can serialize objects with JSON and then store them in localStorage. It's supported by most browsers and can store more data that a cookie. Also doesn't get sent in every request header. Have a look at: https://github.com/johnhunter/netcache/localstore.js for an example of implementation.

johnhunter
  • 1,826
  • 15
  • 19