gekrish
gekrish

Reputation: 2191

How to store a custom javascript Object in HTML DOM?

If I create a custom javascript Object using a constructor, Is it possible to persist the object between HTTP Requests? - like storing it in the DOM and use it conditionally ?

Will the DOM Objects persist (all client side Objects) between the HTTP Requests ..? or will it get lost after every form submit..?

Thanks

Upvotes: 2

Views: 2265

Answers (3)

keymone
keymone

Reputation: 8104

  1. you can store object in cookie using JSON to serialize it

  2. you can use experimental HTML5 persistent storage: http://dev.w3.org/html5/webstorage/

  3. you can ask people to install plugin like Google Gears which enables persistent storage

Upvotes: 1

Oded
Oded

Reputation: 499012

It will get lost on every request.

If it is very small, you might be able to put it in a cookie and re-read it (and evel it) on every reload.

With HTML5 you should be able to persist it using web/local storage.

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074385

If you're refreshing the page, then the objects on that page will be released and the new page won't have access to them. You do have some options though.

  1. You can use frames and only refresh the "main" frame. The objects stored in the JavaScript code and/or window object of the other frame(s) will be unchanged. These could be traditional frames or iframes.

  2. You can serialize your objects out to a string (perhaps a JSON string) and store them in cookies, which the refreshed page will have access to and can deserialize back into an object graph.

  3. On modern browsers you may have access to web storage in the form of web storage (Google Gears is one implementation) which is backed by an SQLite database (or any database implementing the web storage API, which at the moment is pretty much an SQLite database — this is one of the things holding up the web storage API, in fact, the lack of a second implementation). This also involves serializing/deserializing.

Upvotes: 3

Related Questions