如何检测是否启用了IndexedDB

时间:2015-12-28 15:18:22

标签: javascript browser-feature-detection

我想知道如何在JavaScript中检测IndexedDB是否可用并启用。

我目前正在按照这些方针进行测试:

>>> lst = [3,1,2,2,1,3,6,7,5,4,8]
>>> quicksort(lst, 0, len(lst)-1)
>>> lst
[1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 8]

但我更倾向于使用特征检测,而不是依赖于版本号。

假设用于测试IndexedDB的功能检测是有效的,有没有人知道在页面加载时测试它的好方法?

2 个答案:

答案 0 :(得分:5)

您可以使用your updated link

进行检查
if (Modernizr.indexeddb) {
  console.log("IndexedDB is supported.");
} else {
  console.log("IndexedDB is not supprorted.");
}

请注意,现在几乎所有现代浏览器都支持IndexedDB(请参阅Modernizr)。

答案 1 :(得分:3)

使用MDN中的一些代码,您还需要填写其他一些功能:

// 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 ) {
  alert("No DB");
}