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.
open(name)
open(name, version)
-
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
.
Experimental Gecko options object
-
options (version and storage)
Optional
Deprecated
-
In Gecko, since version 26, you can include a non-standard options
object as a parameter of IDBFactory.open
that contains the version
number of the database, plus a storage value that specifies whether you want to use persistent
or temporary
storage.
Warning: The storage
attribute is deprecated and will soon be removed from Gecko. You should use StorageManager.persist()
to get persistent storage instead.
A IDBOpenDBRequest
object on which subsequent events related to this request are fired.
This method may raise a DOMException
of the following types:
Example of calling open
with the current specification's version
parameter:
var 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 example live.)
var note = document.querySelector("ul");
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction;
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange;
var DBOpenRequest = window.indexedDB.open("toDoList", 4);
DBOpenRequest.onerror = function(event) {
note.innerHTML += '<li>Error loading database.</li>';
};
DBOpenRequest.onsuccess = function(event) {
note.innerHTML += '<li>Database initialized.</li>';
db = DBOpenRequest.result;
};