HTML DOM indexedDB open() Method

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.