Game Development Reference
In-Depth Information
Note
As mentioned earlier, the only real difference between local storage and session
storage is the fact that session storage clears itself whenever the browser closes
down. Besides that, everything about the two is exactly the same. Thus, learn-
ing how to use both will be a simple experience, since learning one also means
learning the other. However, knowing when to use one over the other might take
a bit more thinking on your part. For best results, try to focus on the unique char-
acteristics and needs of your own application before deciding which one to use.
More importantly, realize that it is perfectly legal to use both storage systems in
the same application. The key is to focus on a unique feature, and decide what
storage API best suits those specific needs.
Both the local storage and session storage objects are instances of the class St-
orage . The interface defined by the storage class, through which we can interact
with these storage objects, is defined as follows (source: Web Storage W3C Candid-
ate Recommendation, December 08, 2011, http://www.w3.org/TR/webstorage/ ) :
getItem(key) : It returns the current value associated with the given
key. If the given key does not exist in the list associated with the object
then this method must return null.
setItem(key, value) : It first checks if a key/value pair with the given
key already exists in the list associated with the object. If it does not, then
a new key/value pair must be added to the list, with the given key and
with its value set to value . If the given key does exist in the list, then it
must have its value updated to value . If it couldn't set the new value, the
method must throw a QuotaExceededError exception. (Setting could
fail if, for example, the user has disabled storage for the site, or if the
quota has been exceeded.)
removeItem(key) : It causes the key/value pair with the given key to be
removed from the list associated with the object, if it exists. If no item with
that key exists, the method must do nothing.
clear() : It automatically causes the list associated with the object to be
emptied of all key/value pairs, if there are any. If there are none, then the
method must do nothing.
Search WWH ::




Custom Search