25.02.2013 Views

Peter Lubbers - Pro HTML 5 Programming

Pro HTML 5 Programming

Pro HTML 5 Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

246<br />

CHAPTER 10 ■ CREATING <strong>HTML</strong>5 OFFLINE WEB APPLICATIONS<br />

Due to the varying levels of support, it is a good idea to first test if <strong>HTML</strong>5 offline web applications<br />

are supported before you use these elements.<br />

Using the <strong>HTML</strong>5 Offline Web Application API<br />

In this section, we will explore the specifics of how you can use the Offline Web Applications API.<br />

Checking for Browser Support<br />

Before you try to use the Offline Web Applications API, it is a good idea to check for browser support.<br />

Listing 10-1 shows how you can do that.<br />

Listing 10-1. Checking Browser Support for the Offline Web Applications API<br />

if(window.applicationCache) {<br />

// this browser supports offline applications<br />

}<br />

Creating a Simple Offline Application<br />

Let’s say that you want to create a one-page application that consists of an <strong>HTML</strong> document, a style<br />

sheet, and a JavaScript file. To add offline support to your <strong>HTML</strong>5 application, you include a manifest<br />

attribute on the html element as shown in the Listing 10-2.<br />

Listing 10-2. The manifest Attribute on the <strong>HTML</strong> Element<br />

<br />

<br />

.<br />

.<br />

.<br />

<br />

Alongside the <strong>HTML</strong> document, provide a manifest file specifying which resources to cache. Listing<br />

10-3 shows the contents of an example cache manifest file.<br />

Listing 10-3. Contents of an Example Cache Manifest File<br />

CACHE MANIFEST<br />

example.html<br />

example.js<br />

example.css<br />

example.gif

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!