HTML and CSS Reference
In-Depth Information
if (!dbEng)
alert("IndexedDB is not supported on this browser");
else {
var request ="Chess", 1);
request.onsuccess = function (event) {
db =;
request.onerror = function (event) {
alert("Please allow the browser to open the database");
request.onupgradeneeded = function (event) {
Because the specification has not been finalized, the browsers will be using the vendor prefix. If you can't
access the indexedDB object then the browser does not support it. For this demo you can simply use alert() to
notify the user and stop further processing.
This code then uses the indexedDB object to open the Chess database, specifying that version 1 should be
used. The open() method returns a DBRequest object, as I describer earlier. You will attach three event handlers
for this request:
onsuccess - This event handler simply saves the reference to the database. You will add
more logic here later. Notice that the database is obtained from the
property, which is how all results are returned.
onerror - The primary reason that the browser fails to open a database is that the browser
has the IndexedDB feature blocked. This can be disabled for security reasons. In this case,
the user is prompted to allow access.
onupgradeneeded - This is raised if the database does not exist or if the specified version
is not the current version. This calls the configureDatabase() function, which you'll
implement now.
Defining the Database Structure
Add the code shown in Listing 11-5 to implement the configureDatabase() function.
Listing 11-5. Defining the database structure
function configureDatabase(e) {
alert("Configuring database - current version is "+ e.oldVersion +
", requested version is "+ e.newVersion); db = e.currentTarget.result;
// Remove all existing data stores
var storeList = db.objectStoreNames;
for (var i = 0; i < storeList.length; i++) {
Search WWH ::

Custom Search