HTML and CSS Reference
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
• 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
Adds a key-value pair to a web storage object. If the key already exists, its value is
Retrieves the value of a specified key from a web storage object.
Removes a specified key and the value associated with it from a web storage object.
Removes all the key-value pairs from the web storage object.
Takes a zero-based index and returns key name at that index.
Returns the total number of key-value pairs from the web storage object.
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.