IDBVersionChangeEvent: thuộc tính newVersion

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Note: This feature is available in Web Workers.

Thuộc tính chỉ đọc newVersion của giao diện IDBVersionChangeEvent trả về số phiên bản mới của cơ sở dữ liệu.

Giá trị

Một số là số nguyên 64-bit hoặc null nếu cơ sở dữ liệu đang bị xóa.

Ví dụ

Trong đoạn code sau, chúng ta thực hiện một yêu cầu mở cơ sở dữ liệu và bao gồm các trình xử lý cho các trường hợp thành công và lỗi. Các sự kiện này được kích hoạt thông qua giao diện tùy chỉnh IDBVersionChangeEvent. Để có ví dụ hoạt động đầy đủ, xem ứng dụng To-do Notifications (xem ví dụ trực tiếp).

js
const note = document.querySelector("ul");

// Hãy mở phiên bản 4 của cơ sở dữ liệu
const DBOpenRequest = window.indexedDB.open("toDoList", 4);

// hai trình xử lý sự kiện này hoạt động khi cơ sở dữ liệu được mở
DBOpenRequest.onerror = (event) => {
  note.appendChild(document.createElement("li")).textContent =
    "Error loading database.";
};

DBOpenRequest.onsuccess = (event) => {
  note.appendChild(document.createElement("li")).textContent =
    "Database initialized.";

  // lưu kết quả mở cơ sở dữ liệu vào biến db.
  // Điều này được sử dụng nhiều về sau, để mở giao dịch và những thứ tương tự.
  const db = DBOpenRequest.result;
};

Thông số kỹ thuật

Thông số kỹ thuật
Indexed Database API 3.0
# dom-idbversionchangeevent-newversion

Tương thích trình duyệt

Xem thêm