Technical Evangelist & Editor of Mozilla Hacks. Gives talks & blogs about HTML5, JavaScript & the Open Web. Robert is a strong believer in HTML5 and the Open Web and has been working since 1999 with Front End development for the web - in Sweden and in New York City. He regularly also blogs at robertnyman.com and loves to travel and meet people.
// IndexedDB
window.indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.OIndexedDB || window.msIndexedDB,
IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.OIDBTransaction || window.msIDBTransaction,
dbVersion =1;/*
Note: The recommended way to do this is assigning it to window.indexedDB,
to avoid potential issues in the global scope when web browsers start
removing prefixes in their implementations.
You can assign it to a varible, like var indexedDB… but then you have
to make sure that the code is contained within a function.
*/// Create/open databasevar request = indexedDB.open("elephantFiles", dbVersion);
request.onsuccess=function(event){
console.log("Success creating/accessing IndexedDB database");
db = request.result;
db.onerror=function(event){
console.log("Error creating/accessing IndexedDB database");};// Interim solution for Google Chrome to create an objectStore. Will be deprecatedif(db.setVersion){if(db.version != dbVersion){var setVersion = db.setVersion(dbVersion);
setVersion.onsuccess=function(){createObjectStore(db);getImageFile();};}else{getImageFile();}}else{getImageFile();}}// For future use. Currently only in latest Firefox versions
request.onupgradeneeded=function(event){createObjectStore(event.target.result);};
// Create XHRvar xhr =newXMLHttpRequest(),
blob;
xhr.open("GET","elephant.png",true);// Set the responseType to blob
xhr.responseType ="blob";
xhr.addEventListener("load",function(){if(xhr.status ===200){
console.log("Image retrieved");// File as response
blob = xhr.response;// Put the received blob into IndexedDBputElephantInDb(blob);}},false);// Send XHR
xhr.send();
// Retrieve the file that was just stored
transaction.objectStore("elephants").get("image").onsuccess=function(event){var imgFile = event.target.result;
console.log("Got elephant!"+ imgFile);// Get window.URL objectvarURL= window.URL|| window.webkitURL;// Create and revoke ObjectURLvar imgURL =URL.createObjectURL(imgFile);// Set img src to ObjectURLvar imgElephant = document.getElementById("elephant");
imgElephant.setAttribute("src", imgURL);// Revoking ObjectURLURL.revokeObjectURL(imgURL);};
(function(){// IndexedDBvar indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.OIndexedDB || window.msIndexedDB,
IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.OIDBTransaction || window.msIDBTransaction,
dbVersion =1.0;// Create/open databasevar request = indexedDB.open("elephantFiles", dbVersion),
db,createObjectStore=function(dataBase){// Create an objectStore
console.log("Creating objectStore")
dataBase.createObjectStore("elephants");},getImageFile=function(){// Create XHRvar xhr =newXMLHttpRequest(),
blob;
xhr.open("GET","elephant.png",true);// Set the responseType to blob
xhr.responseType ="blob";
xhr.addEventListener("load",function(){if(xhr.status ===200){
console.log("Image retrieved");// Blob as response
blob = xhr.response;
console.log("Blob:"+ blob);// Put the received blob into IndexedDBputElephantInDb(blob);}},false);// Send XHR
xhr.send();},putElephantInDb=function(blob){
console.log("Putting elephants in IndexedDB");// Open a transaction to the databasevar transaction = db.transaction(["elephants"], IDBTransaction.READ_WRITE);// Put the blob into the dabasevar put = transaction.objectStore("elephants").put(blob,"image");// Retrieve the file that was just stored
transaction.objectStore("elephants").get("image").onsuccess=function(event){var imgFile = event.target.result;
console.log("Got elephant!"+ imgFile);// Get window.URL objectvarURL= window.URL|| window.webkitURL;// Create and revoke ObjectURLvar imgURL =URL.createObjectURL(imgFile);// Set img src to ObjectURLvar imgElephant = document.getElementById("elephant");
imgElephant.setAttribute("src", imgURL);// Revoking ObjectURLURL.revokeObjectURL(imgURL);};};
request.onerror=function(event){
console.log("Error creating/accessing IndexedDB database");};
request.onsuccess=function(event){
console.log("Success creating/accessing IndexedDB database");
db = request.result;
db.onerror=function(event){
console.log("Error creating/accessing IndexedDB database");};// Interim solution for Google Chrome to create an objectStore. Will be deprecatedif(db.setVersion){if(db.version != dbVersion){var setVersion = db.setVersion(dbVersion);
setVersion.onsuccess=function(){createObjectStore(db);getImageFile();};}else{getImageFile();}}else{getImageFile();}}// For future use. Currently only in latest Firefox versions
request.onupgradeneeded=function(event){createObjectStore(event.target.result);};})();