IndexedDB Example
HTML
<div id="results"></div>
CSS
body {
font-family: sans-serif;
padding: 10px;
}
JavaScript
// In the following line, you should include the prefixes of implementations you want to test.
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
// DON'T use "var indexedDB = ..." if you're not in a function.
// Moreover, you may need references to some window.IDB* objects:
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction || {READ_WRITE: "readwrite"}; // This line should only be needed if it is needed to support the object's constants for older browsers
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange;
// (Mozilla has never prefixed these objects, so we don't need window.mozIDB*)
if (!window.indexedDB) {
window.alert("Your browser doesn't support a stable version of IndexedDB. Such and such feature will not be available.");
}
else{
var db;
var request = window.indexedDB.open("MyTestDatabase", 3);
request.onerror = function(event) {
window.alert("error");
};
request.onsuccess = function(event) {
window.alert("Successs");
db = event.target.result;
console.log(db);
// This event is only implemented in recent browsers
request.onupgradeneeded = function(event) {
// Create an objectStore for this database
var objectStore = db.createObjectStore("name", { keyPath: "myKey" });
objectStore.createIndex("name", "name", { unique: false });
objectStore.transaction.oncomplete = function(event) {
// Store values in the newly created objectStore.
var customerObjectStore = db.transaction("customers", "readwrite").objectStore("customers");
customerData.forEach(function(customer) {
customerObjectStore.add(customer);
});
};
};
};
}