--- title: IDBFactory.open slug: Web/API/IDBFactory/open translation_of: Web/API/IDBFactory/open ---
{{APIRef("IDBFactory")}}
IDBFactory.open
方法用于打开一个数据库连接。本方法立即返回一个 {{domxref("IDBOpenDBRequest")}} 对象,但打开数据库的操作是异步执行的。
连接数据库在一个单独的线程中进行,包括以下几个步骤:
version,中止操作并返回
类型为 VersionError 的
DOMError
。version,触发一个
versionchange
操作。如果操作成功执行,将触发 success
事件 on the request object that is returned from this method, with its result
attribute set to the new {{domxref("IDBDatabase")}} object for the connection.
If an error occurs while the database connection is being opened, then an error event is fired on the request object returned from this method.
For the current standard:
IDBOpenDBRequest
open (DOMString name, [EnforceRange] optional unsigned long long version);
For the experimental version with options
(see below):
IDBOpenDBRequest open (DOMString name, optional IDBOpenDBOptions options);
For the current standard:
var request = window.indexedDB.open("toDoList", 4);
For the experimental version with options
(see below):
var request = window.indexedDB.open("toDoList", {version: 4, storage: "temporary"});
options
object as a parameter of {{ domxref("IDBFactory.open") }} that contains the version
number of the database, plus a storage value that specifies whether you want to use permanent
(the default value) storage for the IndexedDB, or temporary
storage (aka shared pool.) See {{ bug("785884") }} for more details. This is a non-standard feature that we are looking to standardise sometime in the future.Note: Data in temporary storage persists until the global limit for the pool is reached. The global limit calculation is relatively complex, but we are considering changing it (see {{ Bug("968272") }}). When the global limit is reached, then data for the least recently used origin is deleted. There's also a group limit (eTLD+1 group/domain) which is currently 20% of the global limit. All requets that would exceed the group limit are just rejected.
This method may raise a {{domxref("DOMException")}} with a DOMError of the following types:
Exception | 描述 |
---|---|
TypeError |
The value of version is zero or a negative number or not a number. |
{{page("/en-US/docs/Web/API/IDBFactory","Specifications")}}
{{page("/en-US/docs/Web/API/IDBFactory","Browser_compatibility")}}