IDBDatabase: name 屬性

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

注意:此功能在 Web Workers 中可用。

IDBDatabase 介面的只讀屬性 name 是一個字串,其中包含已連線資料庫的名稱。

包含已連線資料庫名稱的字串。

示例

本示例展示了開啟資料庫連線、將生成的 IDBDatabase 物件儲存在 db 變數中,然後記錄 name 屬性。有關完整示例,請參閱我們的 待辦事項通知 應用(即時檢視示例)。

js
// Let us open 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 below
  db = DBOpenRequest.result;

  // This line will log the name of the database, which should be "toDoList"
  console.log(db.name);
};

規範

規範
Indexed Database API 3.0
# ref-for-dom-idbdatabase-name①

瀏覽器相容性

另見