HTML and CSS Reference
In-Depth Information
// handle version control
// then create a new object store
request.onerror = function (event) {
alert('Something failed: ' +;
Notice the error handler. In IndexedDB, errors bubble up from
the request up to transaction and up to the database itself. This
means that if an error occurs at any point during any kind of
request, you'll see this alert box. But remember: Alert boxes are
ugly and are not friends with the browser—particularly when
in production; they make the salesmen angry. Make sure you
change the alert box to something like a beautiful, rounded-
corners message that gracefully handles the error. Now, on to
initialising the database.
Version control
The first thing we do once we've opened the database connec-
tion is to handle version control. You can use any string as your
version number, but if you use some logic in how you increment
your versions it might save you a bit of trouble in the future.
Let's call this application version “0.1.” If we make an upgrade,
we can compare “0.1” (the string), which will be the database
version, against “0.2” which would be our upgraded code. Since
this is the first time any of our code has run, and therefore we've
even tried to open a new database, let's check the version and
if it doesn't match, set the version and create the object stores.
I should add that this isn't version control like something like
SVN or Git—if you want to change the number of object stores
in the database, you need to request a version change.
var db = null,
version = '0.1';
request.onsuccess = function (event) {
// cache a copy of the database handle for the future
db =;
// handle version control
if (version != db.version) {
// set the version to 0.1
var verRequest = db.setVersion(version);
Search WWH ::

Custom Search