Wednesday, 31 August 2016

HTML5: How do I store values in the session (and why is $cookie complaining about space).

It appears that there is a size limitation for angular-cookies - 4KB. Bummer.

An alternative is to use the HTML 5 objects window.localStorage or window.sessionStorage. See here for details

I used sessionStorage to hold a single object and an object array from a REST endpoint.

The code to get and set the values is as follows:

First, set the values:
sessionStorage.Object1 = JSON.stringify(singleObject);
sessionStorage.Object2 = JSON.stringify(multipleObjects)

Now populate objects with the cached values:
var single = JSON.parse($.parseJSON(sessionStorage.Object1))
var array = $.parseJSON(sessionStorage.Object2)

No comments:

Post a comment