Web Storage



index
Disabled back button Next Section
printable version

Section 1: Storage Types
Types of web storage.

As indicated in the above figure, web applications can store data on the client device or on the server.

Until HTML5, storage options on the client device consisted of cookies only, but then HTML5 introduced web storage.

Section 2: Cookies
Cookie pic.

A cookie is a text-only string of information that a website transfers to the cookie file of the browser on a client device so that the website can remember who you are.

Cookies contain state information that passes between an origin server and user agent, and that gets stored by the user agent.

Here is a link to Cookie Quick Manager.

Section 3: Setting Cookies
Cookie pic.

JavaScript can create, read, and delete cookies with the document.cookie property.


Cookie Parts

Cookie code. Cookie parts.

Cookie Names are Unique

Cookie code. Cookie names code.

In the next example, the value of user is set to Bob, but then poor Bob is overwritten by Frank.

Cookie code. Cookie names code.

encodeURIComponent

A cookie value cannot contain semicolons, commas, or spaces, so it is recommended that the JavaScript built-in function encodeURIComponent() is used to encode any string that may include these characters before storing it in the cookie.


key-value pairs

To add additional key-value pairs, use separate cookies

Cookie code. Cookie names code.
Cookie code. Cookie names code.

Setting Cookie Expiration

Cookies expire after a certain amount of time, which can be specified when creating the cookie. If no expiration is specified, the cookie will expire when the browser is closed.

Section 4: Local Storage and Session Storage
Web Storage.

Local and Session Storage

Storage Options.

HTML web storage provides two objects for storing data on the client:

Local and Session Storage.

The following links provide further explanation.

Section 5: Local Storage and Session Storage: Storing Data Items

Storage Support

Before using web storage, check browser support for localStorage and sessionStorage:

Storage support check code.

Storage Methods

The Storage Object of the Web Storage API provides access to the session storage or local storage for a particular domain, making it possible for stored data items to be read, added, modified, and deleted.

Local and Session Storage.

Stored data items (Details: Web storage standard)

In the following code segment, a key-value pair is set, then retrieved, then removed.

Local storage example code.

Setting localStorage

Values must be strings only

Each of the approaches in the following figure can be used to store string values:

Setting local storage code.

More details on the localStorage Property are available.

The approach is similar for sessionStorage.


Storing values

It bears repeating that values must be strings only.

In the following code segment, all of the values are converted to and stored as strings. You can see that they are strings by running the storage demo, which uses the typeof operator to find the data type of a JavaScript variable.

function writeStorage() {
if (typeof (Storage) !== "undefined") {

var resultsString = "";

localStorage.setItem("m1", 123);
resultsString += localStorage.m1 + " is a " + typeof localStorage.m1 + "\n";

localStorage["m2"] = true;
resultsString += localStorage.m2 + " is a " + typeof localStorage.m2 + "\n";

localStorage.m3 = 123.67;
resultsString += localStorage.m3 + " is a " + typeof localStorage.m3;
alert(resultsString);

} else {
alert("Web storage not supported.");
}
}

Here is the output:

Example of storing values.

As in the preceding example, each of the values in the following example is converted to a string before it is saved.

This storage demo also produces string output. The code follows:

function writeStorage() {
if (typeof (Storage) !== "undefined") {

var resultsString = "";

localStorage.m4 = null;
resultsString += localStorage.m4 + " is a " + typeof localStorage.m4 + "\n";

localStorage.m5 = [1, 2, 3, 4, 5];
resultsString += localStorage.m5[4] + " is a " + typeof localStorage.m5[4] + "\n";
resultsString += localStorage.m5 + " is a " + typeof localStorage.m5 + "\n";

localStorage.m6 = { name: "Bob", age: 23 };
resultsString += localStorage.m6.age + " is a " + typeof localStorage.m6.age;

alert(resultsString);

} else {
alert("Web storage not supported.");
}
}

Here is the output: Example of storing values.


Object workaround

How can numeric values and other non-string data be stored in web storage when only string values can be stored?

Here is a demo of the object workaround. The code follows:

function writeStorage() {
if (typeof (Storage) !== "undefined") {

var obj = { name: "Bob", age: 23 };
sessionStorage.m7 = JSON.stringify(obj);
var newObj = JSON.parse(sessionStorage.m7);
alert(newObj.name + " is " + newObj.age + " years old");

} else {
alert("Web storage not supported.");
}
}

The output follows:

Example of Object workaround results.

Storage Methods

Both storage mechanisms have the same methods and approaches. Here again is the table of available methods.

Local and Session Storage.

Here is a demo of localStorage and sessionStorage. The code follows.

function writeStorage() {
if (typeof (Storage) !== "undefined") {

var resultsString = "";

localStorage.setItem("course", "INFO2220");
var keyName = localStorage.key(0);
var val = localStorage.getItem("course");
resultsString += "Key '" + keyName + "' is " + val + "\n";
alert("LocalStorage test: " + resultsString);

resultsString = "";
sessionStorage.setItem("instructor", "Parker");
var keyName = sessionStorage.key(0);
var val = sessionStorage.getItem("instructor");
resultsString += "Key '" + keyName + "' is " + val;

alert("SessionStorage test: " + resultsString);

localStorage.removeItem("course");
localStorage.clear();
sessionStorage.removeItem("instructor");
sessionStorage.clear();

} else {
alert("Web storage not supported.");
}
}

Here is the output:

Example of storing values.

More details on sessionStorage.

Section 6: Comparison

The following graphics provide comparisons of the various storage types.

Types of web storage compared. Types of web storage compared. Types of web storage compared.
Section 7: Reference Links