admin管理员组

文章数量:1289548

I am messing around with HTML5's IndexedDB.

Below is a simple code from .html#slide31 that is not working for me in Chrome or Firefox, it is giving me this message in Firebug:

Uncaught TypeError: Cannot call method 'open' of undefined`

for the 1st line of the code, which is:

var db = window.indexedDB.open('FriendDB', 'My Friends!');  // exception here

Can someone help me to get this working please?

There is a JSFiddle running the code


I know this works on the browser because this interactive slide works: .html#slide34

I am messing around with HTML5's IndexedDB.

Below is a simple code from http://html5-demos.appspot./static/html5storage/index.html#slide31 that is not working for me in Chrome or Firefox, it is giving me this message in Firebug:

Uncaught TypeError: Cannot call method 'open' of undefined`

for the 1st line of the code, which is:

var db = window.indexedDB.open('FriendDB', 'My Friends!');  // exception here

Can someone help me to get this working please?

There is a JSFiddle running the code


I know this works on the browser because this interactive slide works: http://html5-demos.appspot./static/html5storage/index.html#slide34

Share Improve this question edited Dec 2, 2011 at 0:09 user166390 asked Dec 1, 2011 at 23:12 JasonDavisJasonDavis 49k107 gold badges326 silver badges558 bronze badges 5
  • window.indexedDB is undefined then. Find the browser/environment that demo is for (a recent Webkit? Safari?). Don't expect it to work elsewhere :) – user166390 Commented Dec 1, 2011 at 23:14
  • @pst I have added a JSFiddle page above. I am running Firefox 8.0.1 and Chrome 15.0.874.121 m the IndexedDB works in both these browsers just fine, just not the code above for me – JasonDavis Commented Dec 1, 2011 at 23:34
  • I wonder if it has to be enabled somehow. I can't get either openDatabase or indexedDB to work, as per hacks.mozilla/2010/06/paring-indexeddb-and-webdatabase – user166390 Commented Dec 1, 2011 at 23:35
  • @pst I don't think so. On this page here html5-demos.appspot./static/html5storage/index.html#slide34 there is a demo you can add and remove items from the DB and it is working in both my browsers – JasonDavis Commented Dec 1, 2011 at 23:52
  • Definitely something else to do with the page. Open up the FF Web-Console on SO: window.indexedDB evaluates to undefined. Open up the FF Web-Console on the linked slide: window.indexedDB evaluates to [object IDBFactory]. – user166390 Commented Dec 2, 2011 at 0:06
Add a ment  | 

3 Answers 3

Reset to default 6

You have to use the prefixed version for each browser (window.webkitIndexedDB or window.mozIndexedDB). Then you can do something like:

window.indexedDB = window.indexedDB
                     || window.webkitIndexedDB
                     || window.mozIndexedDB;

and then use window.indexedDB everywhere in your code.

As far as vendor prefixed extensions in Chrome, if you want to use the full IndexedDB API there's more to it than just window.indexedDB. I'm working on an MIT licensed IndexedDB wrapper. Here's a simplified version of its fixBrowser() method in case it might be of help. It should standardized the interface across Chrome and FF.

InDB.fixBrowser = function () {
    if ( 'webkitIndexedDB' in window ) {
        window.IDBCursor = window.webkitIDBCursor;
        window.IDBDatabase = window.webkitIDBDatabase;
        window.IDBDatabaseError = window.webkitIDBDatabaseError;
        window.IDBDatabaseException = window.webkitIDBDatabaseException;
        window.IDBErrorEvent = window.webkitIDBErrorEvent;
        window.IDBEvent = window.webkitIDBEvent;
        window.IDBFactory = window.webkitIDBFactory;
        window.IDBIndex = window.webkitIDBIndex;
        window.IDBKeyRange = window.webkitIDBKeyRange;
        window.IDBObjectStore = window.webkitIDBObjectStore;
        window.IDBRequest = window.webkitIDBRequest;
        window.IDBSuccessEvent = window.webkitIDBSuccessEvent;
        window.IDBTransaction = window.webkitIDBTransaction;
        window.indexedDB = window.webkitIndexedDB;
    } else if ( 'mozIndexedDB' in window ) {
        window.indexedDB = window.mozIndexedDB;
    }
}

A slightly shorter version (based on names from modernizr):

var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.moz_indexedDB;

本文标签: javascriptWhy does windowindexedDB evaluate to undefined (How to use IndexedDB)Stack Overflow