is a new HTML5 API. Like cookies, it stores string key-value pairs, but unlike cookies:
- Much larger data are allowed: at least 2 MB and usually 5 MB or more per domain.
- Data stay on the client and are not transmitted to or from the server.
- A simple interface is provided to save, retrieve, and remove data.
There are actually two storage objects:
. We will be using the latter.
You can add a new datum or change an existing one with any of these:
localStorage.setItem( "aKey" ) = "aValue";
localStorage[ "aKey" ] = "aValue";
localStorage.aKey = "aValue";
You can read a value with any of these:
var value = localStorage.getItem( "aKey" );
var value = localStorage[ "aKey" ];
var value = localStorage.aKey;
If no value has been assigned for the key,
And you can remove a datum with:
localStorage.removeItem( "aKey" );
You can remove all data with:
The total number of key-value pairs is
, and the keys can be obtained as
localStorage.key( n )
0 <= n < localStorage.length
. So we can iterate through all of the data like this:
for ( i = 0, lim = localStorage.length; i < lim; ++i )
key = localStorage.key( i );
value = localStorage[ key ];
NB: Because local storage is provided at the domain level, and a domain may feature several apps, you should consider using a unique prefix (or similar convention) for the keys for a game.
For the same reason,
may remove more items than you want. The iteration over all keys is also of questionable value, though it might be useful if you want to tie data between games together.