简体   繁体   中英

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

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.

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 .

  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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM