Open In App

HTML DOM indexedDB open() Method

Last Updated : 31 Jul, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The open() method of the indexedDB interface requests opening a connection to a database. This method returns an IDBOpenDBRequest object immediately and performs the open operation asynchronously.

Syntax:

var IDBOpenDBRequest = indexedDB.open(name);
// Or
var IDBOpenDBRequest = indexedDB.open(name, version);

Parameters:  This method accepts two parameters as mentioned above and described below:

  • name: The name of the database to be opened.
  • version (Optional): The version to open the database with.

Return value: This method returns a IDBOpenDBRequest object.

Example: In this example, we will open a database named “toDoList” using this method.

HTML




<!DOCTYPE html>
<html>
<head>
    <title>indexedDB open() method</title>
</head>
<body style="text-align: center;">
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
  
    <p>
        HTML | indexedDB open() method
    </p>
  
    <button onclick="Geeks()">
        Click Here
    </button>
    <p id="a"></p>
  
    <script>
    var a = document.getElementById("a");
    function Geeks() {
        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 DBOpen = window.indexedDB.open("toDoList", 4);
  
        DBOpen.onerror = function (event) {
            a.innerHTML += "<li>Error loading database.</li>";
        };
  
        DBOpen.onsuccess = function (event) {
            a.innerHTML += "<li>Database initialised.</li>";
            console.log(DBOpen);
            console.log(window.indexedDB.databases());
        };
    }
    </script>
</body>
</html>


Output:

Before Button Click:

After Button Click: In the console, IDBOpenDBRequest object can be seen along with database “toDoList” in databases array

Supported Browsers: 

  • Google Chrome
  • Edge
  • Firefox
  • Safari
  • Opera


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads