IDBDatabase.onversionchange

这篇翻译不完整。请帮忙从英语翻译这篇文章

 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
onversionchange
Recommendation  
Indexed Database API 2.0
onversionchange
Recommendation  

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
onversionchangeChrome Full support 24
Full support 24
No support 23 — 24
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge ? Firefox Full support 16
Full support 16
No support 10 — 16
Prefixed
Prefixed Implemented with the vendor prefix: moz
IE Full support 10
Notes
Full support 10
Notes
Notes partial
Opera Full support 15Safari Full support 7WebView Android Full support YesChrome Android Full support 25Edge Mobile ? Firefox Android Full support 22Opera Android Full support 22Safari iOS Full support 8Samsung 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.

更多参考

文档标签和贡献者

此页面的贡献者: seawin
最后编辑者: seawin,