IDBFactory: open() method
The open() method of the IDBFactory interface requests opening a connection to a database.
The method returns an IDBOpenDBRequest object immediately, and performs the open operation asynchronously. If the operation is successful, a success event is fired on the request object that is returned from this method, with its result attribute set to the new IDBDatabase object for the connection.
May trigger upgradeneeded, blocked or versionchange events.
Syntax
open(name)
open(name, version)
Parameters
-
name
-
The name of the database.
version Optional
-
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 version 1.
Return value
A IDBOpenDBRequest object on which subsequent events related to this request are fired.
If the operation is successful, the value of the request's result property is a IDBDatabase object representing the connection to the database.
Exceptions
-
TypeError
-
Thrown if the value of version is not a number greater than zero.
Examples
Example of calling open with the current specification's version parameter:
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 our To-do Notifications app (View the example live).
const note = document.querySelector("ul");
const DBOpenRequest = window.indexedDB.open("toDoList", 4);
DBOpenRequest.onerror = (event) => {
note.innerHTML += "<li>Error loading database.</li>";
};
DBOpenRequest.onsuccess = (event) => {
note.innerHTML += "<li>Database initialized.</li>";
db = DBOpenRequest.result;
};
Specifications
Browser compatibility
|
Desktop |
Mobile |
|
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
WebView Android |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
open |
23 |
12 |
10 |
10 |
15 |
8 |
4.4 |
25 |
22 |
14 |
8 |
1.5 |
options_parameter |
No |
No |
26–104 |
No |
No |
No |
No |
No |
26–104 |
No |
No |
No |
See also