upgradeneeded
event is fired when an attempt was made to open a database with a version number higher than its current version.
| 冒泡 | No |
|---|---|
| 可取消 | No |
| 接口 |
事件
|
| Event handler |
onupgradeneeded
|
This example opens a database and handles the
upgradeneeded
event by making any necessary updates to the object store.
// Open the database
const dBOpenRequest = window.indexedDB.open('toDoList', 4);
dBOpenRequest.addEventListener('upgradeneeded', event => {
const db = event.target.result;
console.log(`Upgrading to version ${db.version}`);
// Create an objectStore for this database
var objectStore = db.createObjectStore('toDoList', { keyPath: 'taskTitle' });
// define what data items the objectStore will contain
objectStore.createIndex('hours', 'hours', { unique: false });
objectStore.createIndex('minutes', 'minutes', { unique: false });
objectStore.createIndex('day', 'day', { unique: false });
objectStore.createIndex('month', 'month', { unique: false });
objectStore.createIndex('year', 'year', { unique: false });
});
This is the same example, but uses the onupgradeneeded event handler property.
// Open the database
const dBOpenRequest = window.indexedDB.open('toDoList', 4);
dBOpenRequest.onupgradeneeded = event => {
const db = event.target.result;
console.log(`Upgrading to version ${db.version}`);
// Create an objectStore for this database
var objectStore = db.createObjectStore('toDoList', { keyPath: 'taskTitle' });
// define what data items the objectStore will contain
objectStore.createIndex('hours', 'hours', { unique: false });
objectStore.createIndex('minutes', 'minutes', { unique: false });
objectStore.createIndex('day', 'day', { unique: false });
objectStore.createIndex('month', 'month', { unique: false });
objectStore.createIndex('year', 'year', { unique: false });
};
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
upgradeneeded
event
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 |
WebView Android
Yes
|
Chrome Android
25
|
Firefox Android 22 | Opera Android 14 | Safari iOS 8 |
Samsung Internet Android
1.5
|
完整支持
部分支持
要求使用供应商前缀或不同名称。
onupgradeneeded
event handler property
IDBOpenDBRequest
blocked
upgradeneeded
IDBCursor
IDBCursorSync
IDBCursorWithValue
IDBDatabase
IDBDatabaseException
IDBDatabaseSync
IDBEnvironment
IDBEnvironmentSync
IDBFactory
IDBFactorySync
IDBIndex
IDBIndexSync
IDBKeyRange
IDBObjectStore
IDBObjectStoreSync
IDBRequest
IDBTransaction
IDBTransactionSync
IDBVersionChangeEvent
IDBVersionChangeRequest