Training Guide: Programming in HTML5 with JavaScript and CSS3 Ebook

(Nora) #1

555


CHAPTER 15 Local data with web storage


S


o far, you’ve learned how to take advantage of a wide array of tools to produce applica-
tions that are aesthetically appealing, responsive, and fast. However, your primary means
of data storage has been confined to sending information back to the server, which requires
the application to wait for a round-trip to occur.
To minimize the cost of relying entirely on server-side persistence, most modern brows-
ers now support web storage (also known as DOM storage), a relatively new feature that
enables storing small amounts of user data on the client machine.
This chapter begins with an overview of the two storage mechanisms (localStorage
and sessionStorage) and how they can make dramatic improvements in how user data is
retained. The chapter then examines how the use of storage events can combat complex
problems such as race conditions.

Lessons in this chapter:
■■Lesson 1: Introducing web storage 555
■■Lesson 2: Handling storage events 565

Before you begin


To complete this book, you must have some understanding of web development. This chap-
ter requires the hardware and software listed in the “System requirements” section in the
book’s Introduction. Specifically, you need a browser (such as Internet Explorer 8 or a newer
version) that can accommodate web storage.

Lesson 1: Introducing web storage


Most web applications rely on some method of data storage, which usually involves a
server-side solution such as a SQL Server database. However, in many scenarios, that might
be excessive, and the ability to store simple, non-sensitive data in your browser would easily
meet your needs.
Free download pdf