HTML5 Guidelines for Web Developers

(coco) #1

208 Chapter 8—Web Storage and Offline Web Applications


arrays or objects, they have to be converted to strings first. An elegant way of do-
ing this is via the JSON library:

JSON.stringify(itemsObject)

How much disk space the browser should reserve for the website is only hinted
at in the specification. The recommended limit for the storage space that can be
used per origin is 5MB (see section 8.1.3, “localStorage”). The current browser
implementations adhere to this recommendation.

8.1.2 “sessionStorage”


One problem with using cookies is that the cookie is directly connected to the
website and is independent of the browser window. The problem can become
acute in the following example: A web shop saves the desired shopping cart in
a cookie on the browser. If you open a second browser window while shopping
and start shopping under a different name in that window, the products in the
original window may change as well.
Although cookies can apply to several windows, the validity of sessionStorage is
limited to the current browser window, which can be desirable in many cases. Fig-
ure 8.1 shows the difference between the two approaches using a simple example.

Figure 8.1 Two windows demonstrating the difference between “sessionStorage” and cookies
Free download pdf