IDBRequest: success イベント

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

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

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

// データベースをオープンする
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 イベントハンドラープロパティを使用した例です。

// データベースをオープンする
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('データベースを正常に開きました!');
};

ブラウザーの互換性

BCD tables only load in the browser

関連情報