localStorage
would be easiest:
localStorage.setItem('a', 1); // or localStorage.a = 1
localStorage.setItem('b', 2);
and to retrieve:
var a = +localStorage.getItem('a'); // or just a = +localStorage.a
Note that localStorage
can only store strings, hence the +
operator in that last line to cast the stored value back into a number. If you want to store objects, use JSON.stringify
and JSON.parse
.
Whether to use the direct properties or setItem
style access is mostly a matter of personal choice. That said, in theory the methods are more rigorous and recommended as they avoid any possibility of confusing the built-in properties of the localStorage
object with the keys that you're trying to store. Also, the methods can be shimmed on older browsers but direct property access cannot.