IDBDatabase
中的 onversionchange
事件处理器能处理版本更新事件,此事件能在任意地方 (很可能在同一台计算机上的另一个窗口/选项卡中)导致数据库结构更改(IDBOpenDBRequest.onupgradeneeded
事件 或 IDBFactory.deleteDatabase
事件)的时候被触发 。
onversionchange
与 versionchange
是不相同的事件(但两者是有关联的)。
Note: 此特性在 Web Worker 中可用。
语法
IDBDatabase.onversionchange = function(event) { ... }
举例
本例展示了一个创建新对象仓库的 IDBOpenDBRequest.onupgradeneeded
代码块;代码中包含用于处理失败操作的 onerror
和 onabort
函数,以及一个 onversionchange 函数用以在数据库结构被改变时通知用户。
request.onupgradeneeded = function(event) { var db = event.target.result; db.onerror = function(event) { note.innerHTML += '<li>Error opening database.</li>'; }; db.onabort = function(event) { note.innerHTML += '<li>Database opening aborted!</li>'; }; // 给这个数据库创建对象仓库 var objectStore = db.createObjectStore("toDoList", { keyPath: "taskTitle" }); // 定义对象仓库中包含的数据项 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 }); objectStore.createIndex("notified", "notified", { unique: false }); note.innerHTML += '<li>Object store created.</li>'; db.onversionchange = function(event) { note.innerHTML += '<li>a database change has occurred; you should refresh this browser window, or close it down and use the other open version of this application, wherever it exists.</li>'; }; };
格式
格式 | 状态 | 注释 |
---|---|---|
Indexed Database API 2.0 onversionchange |
Recommendation | |
Indexed Database API Draft onversionchange |
Recommendation |
浏览器兼容性
本页面的兼容性表生成自结构化数据。如果你想对该数据做出贡献, 请查看 https://github.com/mdn/browser-compat-data 并向我们发送请求。
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
onversionchange | Chrome
Full support
24
| Edge ? | Firefox
Full support
16
| IE
Full support
10
| Opera Full support 15 | Safari Full support 7 | WebView Android Full support Yes | Chrome Android Full support 25 | Firefox Android Full support 22 | Opera Android Full support 14 | Safari iOS Full support 8 | Samsung Internet Android Full support Yes |
Legend
- Full support
- Full support
- Compatibility unknown
- Compatibility unknown
- See implementation notes.
- See implementation notes.
- Requires a vendor prefix or different name for use.
- Requires a vendor prefix or different name for use.
更多参考
- 使用 IndexedDB
- 开始了解事务:
IDBDatabase
- 使用事务:
IDBTransaction
- 设置键值范围:
IDBKeyRange
- 检索与修改数据:
IDBObjectStore
- 使用游标:
IDBCursor
- 参考用例: To-do Notifications (view example live.)
versionchange
事件