Overview

Chapter 5. Mobile applications: client storage and offline execution

HTML5 makes mobile web applications far more capable by giving them practical ways to store data locally and keep working without a live network connection. The chapter centers on a sample app built for mobile use, showing how client-side storage can replace constant server dependence and improve speed, responsiveness, and reliability in low-connectivity situations.

The first major focus is on organizing the app itself and managing simple user preferences with the Web Storage API. Using a single-page structure with multiple views, the app switches between task list, task entry, and settings screens by changing the page state with the URL hash and showing or hiding sections with CSS. Local storage is then used to save and restore settings such as the user’s name and color scheme, and to clear that data when needed.

The chapter then moves to richer client-side data management with IndexedDB, using a task database to support adding, searching, updating, and deleting records. It explains how to detect browser support, create a database and object store, use indexes and cursors for searching, and provide a fallback for browsers that lack IndexedDB. Finally, it introduces the application cache manifest so the entire app can be cached for offline use, and shows how to detect manifest changes and trigger updates automatically when a newer version is available.

The three main views of the My Tasks application: Task List, Add Task, and Settings. To select a view, the application includes a navigation bar near the top.
Listing 5.1. index.html—Application HTML structure
Listing 5.2. index.html—Adding a navigation bar
Listing 5.3. index.html—Main application views
Listing 5.4. app.js—Foundation JavaScript code for the application
The application highlights the current view by displaying a navigation button with a darker background and blue text.
Step 1: Reading application settings from localStorage
Listing 5.5. app.js—Reading data from localStorage

5.2.2. Saving data to localStorage

Listing 5.6. app.js—Saving data to localStorage

5.2.3. Deleting data from localStorage

Listing 5.7. app.js—Clearing data from localStorage
Listing 5.8. app.js—Connecting the UI to the localStorage functions
The user fills out the Settings form and presses the Save Settings button. When the data has been saved to localStorage, the settings are reloaded, and a message is displayed to the user. When the user dismisses this message, they are taken back to the Task List view (which is empty for now).
Listing 5.9. app.js—Feature detection for database-related objects

5.3.2. Creating or connecting to an IndexedDB database, creating an obje- ect store and index

Hierarchical structure of an IndexedDB database. Each database can have many object stores, which themselves can contain many objects. The object is the structure for a data record, equivalent to a row in a relational database.
Listing 5.10. app.js—Connecting to and configuring the database
Each task item has two components that allow the user to update the task list. Checking the box on the left-hand side will mark the task as complete, whereas pressing the red Delete button on the right-hand side will remove the task.
Listing 5.11. app.js—Generating the markup for task items

5.3.4. Searching an IndexedDB database

Listing 5.12. app.js—Searching the database and displaying the resulting tasks
Listing 5.13. app.js—Searching for tasks
In the left screenshot, the application finds no tasks in the database. Therefore, it displays a message and links the question “Add one?” to the Add Task form. In the right screenshot, if you try to search for a task, you’ll see that no tasks match your query, no matter what you enter.

5.3.5. Adding data to a database using IndexedDB or Web SQL

Listing 5.14. app.js—Adding new tasks
Listing 5.15. app.js—Updating and deleting tasks

5.3.7. Dropping a database using IndexedDB

Listing 5.16. app.js—Dropping the database
Listing 5.17. tasks.appcache—Defining resources that are available offline
Step 3: Detect changes in the My Tasks application cache manifest file
Listing 5.18. app.js—Automatic update detection and loading
My Tasks application running offline. You’ll notice the airplane icon in the top left indicating that the phone has no network access. You may also notice that the jazzy font we used in the heading is no longer showing; this font was loaded from the Google Font API, which isn’t available when you’re offline.
When the manifest file has been updated and a new application cache has been created, the updateready event is fired. The application attaches a handler to this event that swaps the old cache for the new cache, then asks the user if they want to update the application (this simply reloads the page, which loads the latest application version from the new cache).

FAQ

What is the main goal of Chapter 5’s My Tasks application?The My Tasks app is a mobile web application that stores data on the client side and works fully offline. It lets users create, update, delete, and search tasks, and also change app settings like name and color scheme.
When should you use Web Storage instead of IndexedDB in this chapter?Use Web Storage for small, simple key/value data such as user preferences and settings. In My Tasks, it stores items like the user’s name and preferred color scheme, while IndexedDB is used for structured task records.
What is the difference between localStorage and sessionStorage?Both are part of the Web Storage API, but localStorage persists between browser sessions, while sessionStorage is cleared when the browser session ends. Their APIs are otherwise the same.
How does My Tasks switch between the Task List, Add Task, and Settings views?The application uses a single HTML page with three section-based views. JavaScript changes the body element’s class through location.hash navigation, and CSS rules show only the matching section.
Why is location.hash used for navigation in My Tasks?location.hash is used because it provides a simple, fast way to switch views within a single-page application. When the hash changes, the app responds by updating the body class and showing the correct view.
How does the app read and save settings with localStorage?The app uses getItem() to load saved values such as the user name and color scheme, and setItem() to store them. This lets settings persist across browser restarts unless the user clears storage.
How does the app clear all saved settings and data?For settings, the app uses localStorage.clear() to remove all stored values. It also calls dropDatabase() to remove all task data, so resetting returns the app to its default state.
What role does IndexedDB play in My Tasks?IndexedDB stores the task data in a structured client-side database. It allows the app to add, update, delete, and search task records efficiently, using object stores and indexes.
How does My Tasks handle browsers that do not support IndexedDB?The chapter uses Web SQL as a fallback when IndexedDB is unavailable. This ensures the app can still store and manage task data on browsers with older support.
How does the application cache manifest make My Tasks work offline?The application cache manifest lists the resources the browser should cache locally, such as HTML, CSS, and JavaScript files. Once cached, the app can load and run offline, and the updateready event can detect changes to refresh the cache when needed.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • HTML5 in Action HTML5 in Action ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • HTML5 in Action HTML5 in Action ebook for free