HTML and CSS Reference
In-Depth Information
any of the previously stored data. Session storage is suitable for a single transaction. Local storage, unlike
session storage, stores data across multiple instances of the browser and also beyond the current session.
In summary, web storage is good choice if
• You need to store data exceeding the size limits of cookie-based storage.
• You don't need to pass data to and from the server with every request.
• You don't need to set any speciic expiration time for the data.
However, web storage may not be a good choice if
• You wish to store a huge amount of data.
• Your data can't be easily stored as key-value pairs (binary data or BLOBS, for
example).
• Data to be stored is sensitive.
Session Storage and Local Storage Objects
As mentioned earlier, sessionStorage and localStorage objects store data as key-value pairs. Both of these
objects have similar properties and methods. Table 7-1 lists them for your quick reference.
Table 7-1. Properties and Methods of sessionStorage and localStorage
Property / Method
Description
setItem()
Adds a key-value pair to a web storage object. If the key already exists, its value is
changed.
getItem()
Retrieves the value of a specified key from a web storage object.
removeItem()
Removes a specified key and the value associated with it from a web storage object.
clear()
Removes all the key-value pairs from the web storage object.
key()
Takes a zero-based index and returns key name at that index.
length
Returns the total number of key-value pairs from the web storage object.
remainingSpace
Returns the amount of storage space (in bytes) still available for storing data. This
property is currently supported only by IE.
Now that you have some idea about session storage and local storage, let's try these properties and
methods in a Web Forms application.
n Note You can also test the sessionStorage and localStorage objects using plain HTML pages, but in that
case you must host them in Internet Information Services (IIS). Unless your pages are part of a web site, the browser
can't determine their originating domain and thus can't allocate storage space to them. You learn about these
security restrictions later in this chapter.
 
Search WWH ::




Custom Search