Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

IDBFactory: open() method

BaselineWidely available

Note: This feature is available inWeb Workers.

Theopen() method of theIDBFactory interface requests opening aconnection to a database.

The method returns anIDBOpenDBRequest object immediately, and performs the open operation asynchronously.If the operation is successful, asuccess event is fired on the request object that is returned from this method, with itsresult attribute set to the newIDBDatabase object for the connection.

May triggerupgradeneeded,blocked orversionchange events.

Syntax

js
open(name)open(name, version)

Parameters

name

The name of the database.

versionOptional

Optional. The version to open the database with. If the version is not provided and the database exists, then a connection to the database will be opened without changing its version.If the version is not provided and the database does not exist, then it will be created with version1.

Return value

AIDBOpenDBRequest object on which subsequent events related to this request are fired.

If the operation is successful, the value of the request'sresult property is aIDBDatabase object representing the connection to the database.

Exceptions

TypeError

Thrown if the value ofversion is not a number greater than zero.

Examples

Example of callingopen with the current specification'sversion parameter:

js
const request = window.indexedDB.open("toDoList", 4);

In the following code snippet, we make a request to open a database, and include handlers for the success and error cases.For a full working example, see ourTo-do Notifications app (View the example live).

js
const note = document.querySelector("ul");// Let us open version 4 of our databaseconst DBOpenRequest = window.indexedDB.open("toDoList", 4);// these two event handlers act on the database being opened// successfully, or notDBOpenRequest.onerror = (event) => {  note.appendChild(document.createElement("li")).textContent =    "Error loading database.";};DBOpenRequest.onsuccess = (event) => {  note.appendChild(document.createElement("li")).textContent =    "Database initialized.";  // store the result of opening the database in the db  // variable. This is used a lot later on, for opening  // transactions and suchlike.  db = DBOpenRequest.result;};

Specifications

Specification
Indexed Database API 3.0
# ref-for-dom-idbfactory-open②

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp