如何从JavaScript中删除整个IndexedDB数据库?

akn*_*ds1 40 javascript html5 phantomjs indexeddb

如何从JavaScript中删除整个IndexedDB数据库,而不仅仅是一个对象存储?我正在使用IndexedDB垫片,它可能使用WebSQL作为其后端.

我主要想知道如何为PhantomJS(无头)浏览器执行此操作,尽管Chrome,Safari(在iPad上)和IE10是其他重要的浏览器.

akn*_*ds1 69

据我所知,应该使用indexedDB.deleteDatabase:

var req = indexedDB.deleteDatabase(databaseName);
req.onsuccess = function () {
    console.log("Deleted database successfully");
};
req.onerror = function () {
    console.log("Couldn't delete database");
};
req.onblocked = function () {
    console.log("Couldn't delete database due to the operation being blocked");
};
Run Code Online (Sandbox Code Playgroud)

我可以确认它适用于PhantomJS 1.9.0和Chrome 26.0.1410.43.

  • 您还应该为被阻止的事件添加句柄:req.onblocked = function(){console.log("deleteDatbase got blocked event"); } (7认同)
  • 是的,这就是你想要的,我在db.js的自动化测试中广泛使用它 - https://github.com/aaronpowell/db.js/blob/master/tests/specs/indexes.js#L15 (3认同)