HTML5 and CSS3, Second Edition

(singke) #1
those browsers do support Web SQL Databases. To use IndexedDBShim, we
download the minified version and load it in the <head> section of the page:

html5_indexedDB/index.html
<scriptsrc="javascripts/IndexedDBShim.min.js"></script>

It has to be in the <head> section of the page or its features might not be
available right away in Safari, causing things to break wildly. You could use
Modernizr to load this library only when needed, but that raises the question
of whether it makes sense to load another library just to load one that does
its own detection anyway. If you’re going to use Modernizr for something else,
then it absolutely makes sense to use it to load all libraries on demand.

With this fallback in place, when we load this app in Safari or other browsers
that support Web SQL databases, things just work. This fallback solution
isn’t perfect, so you’ll want to test it carefully, but it is a workable and widely
supported solution for browsers that support Web SQL.

Unfortunately, there is no fallback for IndexedDB for Internet Explorer 8 and



  1. Users of those browsers will be left out of the fun.


We can store data on the client’s browser, but we can also create apps that
work without an active Internet connection. Let’s explore offline applications.

Web SQL Databases—Awesome but Stalled


The Web SQL Databases draft specification defines an API that lets developers use a
database in the browser; the database is based on SQLite, the popular open source
database that’s often used on the iOS and Android platforms. This API is implemented
in Safari, Safari on iOS, the Android browsers, and Chrome, and is extremely easy
to use if you understand how to write SQL statements.

Unfortunately, because the Web SQL Databases specification focused on only a single
implementation, the standards committee decided it shouldn’t move forward unless
someone brought forth a competing implementation. Mozilla and Microsoft decided
to move ahead with IndexedDB instead, and Google added IndexedDB to recent ver-
sions of Chrome.

However, if you’d like to see how Web SQL databases work, you can find an imple-
mentation of this notes application using Web SQL databases in the html5_websql
folder within the downloadable source code for this book. This specification could be
extremely useful if you’re developing applications on platforms that use this technol-
ogy, even if the specification itself is stalled.

Chapter 9. Saving Data on the Client • 202


Download from Wow! eBook <www.wowebook.com> report erratum • discuss

Free download pdf