IDBFactory: open() method

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.

Note: This feature is available in Web Workers.

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

js
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

An 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 an 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:

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 our To-do Notifications app (View the example live).

js
const note = document.querySelector("ul");

// Let us open version 4 of our database
const DBOpenRequest = window.indexedDB.open("toDoList", 4);

// these two event handlers act on the database being opened
// successfully, or not
DBOpenRequest.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

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
open
Chrome – Full support
Chrome 23 (Release date: 2012-11-06)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 10 (Release date: 2012-01-31)
footnote Full support
Opera – Full support
Opera 15 (Release date: 2013-07-02)
footnote Full support
Safari – No support
Safari 8 – 13.1 (Release date: 2014-10-16)
unknown Support unknown
Safari – Partial support
Safari 14 – 14.1 (Release date: 2020-09-16)
footnote Partial support
footnote In some releases of Safari 14, the first indexedDB.open() call hangs forever, see bug 226547.
Safari – Full support
Safari 15 (Release date: 2021-09-20)
footnote Full support
Chrome Android – Full support
Chrome Android 25 (Release date: 2013-02-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 22 (Release date: 2013-06-25)
footnote Full support
Opera Android – Full support
Opera Android 14 (Release date: 2013-05-21)
footnote Full support
Safari on iOS – No support
Safari on iOS 8 – 13.4 (Release date: 2014-09-17)
unknown Support unknown
Safari on iOS – Partial support
Safari on iOS 14 – 14.5 (Release date: 2020-09-16)
footnote Partial support
footnote In some releases of Safari on iOS 14, the first indexedDB.open() call hangs forever, see bug 226547.
Safari on iOS – Full support
Safari on iOS 15 (Release date: 2021-09-20)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1.5 (Release date: 2013-09-25)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – No support
WebView on iOS 8 – 13.4 (Release date: 2014-09-17)
unknown Support unknown
WebView on iOS – Partial support
WebView on iOS 14 – 14.5 (Release date: 2020-09-16)
footnote Partial support
footnote In some releases of WebView on iOS 14, the first indexedDB.open() call hangs forever, see bug 226547.
WebView on iOS – Full support
WebView on iOS 15 (Release date: 2021-09-20)
footnote Full support

Legend

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

Full support
Full support
Partial support
Partial support
Has more compatibility info.

See also