Game Development Reference
In-Depth Information
ded to fill the role of object persistence (but rather, simple key-value string pairs), this
should be seen as nothing more than a limited, yet very neat trick.
Session storage
Since the sessionStorage interface is identical to that of localStorage, there is no
reason to repeat all of the information just described. For a more in-depth discussion
about sessionStorage, look at the two previous sections, and replace the word "loc-
al" with "session". Everything mentioned above that applies to local storage is also
true for session storage. Again, the only difference between the two is that any data
saved on sessionStorage is erased when the session with the client ends (that is,
whenever the browser is shut down).
Some examples of how to use sessionStorage will be shown below. In the example,
we will attempt to store a value in the sessionStorage if that value doesn't already
exist. Remember, when we set a key-value pair to the storage, if that key already
exists in the storage, then whatever value was associated with that key will be over-
written. If the key doesn't exist, it gets created automatically.
var name =
sessionStorage.getItem("coolestPerson");
// Only set a new value if the key exists,
// and the value is not what we want
if (name != null && name != "Rodrigo") {
sessionStorage.setItem("coolestPerson",
"Rodrigo");
}
Note that we can also query the sessionStorage object for a specific key using the
in operator, which returns a Boolean value shown as follows:
if ("coolestPerson" in sessionStorage) {
// …
}
Finally, although we can check the total amount of keys in the storage through ses-
sionStorage.length , that by itself may not be very useful if we don't know what
Search WWH ::




Custom Search