IDBRequest: success イベント

Baseline Widely available

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

success イベントは IDBRequest が成功すると発生します。

バブリングする いいえ
キャンセル可能 いいえ
インターフェイス Event
イベントハンドラープロパティ onsuccess

この例では、データベースをオープンします。その success イベントを addEventListener() でリッスンします。

js
// データベースをオープンする
const openRequest = window.indexedDB.open("toDoList", 4);

openRequest.onupgradeneeded = (event) => {
  const db = event.target.result;

  db.onerror = () => {
    console.log("データベースの作成中にエラー発生");
  };

  // オブジェクトストアを作成する
  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 });
};

openRequest.addEventListener("success", (event) => {
  console.log("データベースを正常に開きました!");
});

下記は同じことを onsuccess イベントハンドラープロパティを使用した例です。

js
// データベースをオープンする
const openRequest = window.indexedDB.open("toDoList", 4);

openRequest.onupgradeneeded = (event) => {
  const db = event.target.result;

  db.onerror = () => {
    console.log("データベースの作成中にエラー発生");
  };

  // オブジェクトストアを作成する
  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 });
};

openRequest.onsuccess = (event) => {
  console.log("データベースを正常に開きました!");
};

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
success event

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報