如何检测是否启用了 IndexedDB
How to detect if IndexedDB is enabled
我想知道如何在 JavaScript 中检测 IndexedDB 是否可用和启用。
我目前正在按照这些思路进行测试:
if (window.indexedDB) {
if ((using_chrome && browserVersion >= 24)
|| (usingFirefox && browserVersion >= 16)
|| (usingIe && browserVersion >= 10)
|| (usingEdge && browserVersion >= 12)
|| (usingSafari && browserVersion >= 9)) {
accessible = true;
}
}
但我更愿意使用功能检测而不是依赖版本号。
假设用于测试 IndexedDB 的特征检测是有效的,有人知道在页面加载时测试它的好方法吗?
您可以使用 Modernizr 进行检查:
if (Modernizr.indexeddb) {
console.log("IndexedDB is supported.");
} else {
console.log("IndexedDB is not supprorted.");
}
请注意,现在几乎所有现代浏览器都支持 IndexedDB(参见 caniuse.com)。
使用 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");
}
我想知道如何在 JavaScript 中检测 IndexedDB 是否可用和启用。
我目前正在按照这些思路进行测试:
if (window.indexedDB) {
if ((using_chrome && browserVersion >= 24)
|| (usingFirefox && browserVersion >= 16)
|| (usingIe && browserVersion >= 10)
|| (usingEdge && browserVersion >= 12)
|| (usingSafari && browserVersion >= 9)) {
accessible = true;
}
}
但我更愿意使用功能检测而不是依赖版本号。
假设用于测试 IndexedDB 的特征检测是有效的,有人知道在页面加载时测试它的好方法吗?
您可以使用 Modernizr 进行检查:
if (Modernizr.indexeddb) {
console.log("IndexedDB is supported.");
} else {
console.log("IndexedDB is not supprorted.");
}
请注意,现在几乎所有现代浏览器都支持 IndexedDB(参见 caniuse.com)。
使用 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");
}