Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <button id="set">Set</button>
  <button id="get">Get</button>
  <p id="log"></p>
  <script src="https://unpkg.com/idb-keyval@2.3.0"></script>
  <script>
    const KEY = 'example-key';
    const VALUE = 'example-value';
    
    function log(message) {
      document.querySelector('#log').innerText = message;
    }
    
    async function get() {
      return await idbKeyval.get(KEY);
    }
    
    async function set() {
      await idbKeyval.set(KEY, VALUE);
    }
    
    document.querySelector('#set').addEventListener('click', async () => {
      await set();
      log(`Key ${KEY} was saved with value ${VALUE}.`);
    });
    
    document.querySelector('#get').addEventListener('click', async () => {
      const value = await get();
      log(`Key ${KEY} was read with value ${value}.`);
    });
  </script>
</body>
</html>
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers