success
イベントはIDBRequest
が成功すると着火します。
Bubbles | No |
---|---|
Cancelable | No |
Interface | Event |
Event handler property | onsuccess |
例
この例では、データベースをオープンします。そのsuccess
イベントをaddEventListener()
でリスンします。
// データベースをオープンする
const openRequest = window.indexedDB.open('toDoList', 4);
openRequest.onupgradeneeded = (event) => {
const db = event.target.result;
db.onerror = () => {
console.log('Error creating database');
};
// オブジェクトストアを作成する
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('Database opened successfully!');
});
下記は同じことをonsuccess
イベントハンドラープロパティを使用した例です。
// データベースをオープンする
const openRequest = window.indexedDB.open('toDoList', 4);
openRequest.onupgradeneeded = (event) => {
const db = event.target.result;
db.onerror = () => {
console.log('Error creating database');
};
// オブジェクトストアを作成する
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('Database opened successfully!');
};
ブラウザの対応
BCD tables only load in the browser
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
関連情報
- Using IndexedDB
onsuccess
event handler property