Purge IndexedDB data on tab closure

Storing data in indexedDB instead of sessionStorage is necessary due to the size exceeding 5 MB.

I'm uncertain about the cleanup strategy. I want the data to persist during page reloads or navigation, but I'd like it removed if the user closes the browser tab to save space.

What is the best way to achieve this? It needs to be functional at least in Chrome.

Answer №1

To prevent unnecessary database clutter, one approach is to store an indicator in the session storage. If this indicator is not present, you can then proceed to delete the designated database.

(async() =>
{
  if (!sessionStorage.getItem('just-a-placeholder'))
  {
    indexedDB.deleteDatabase('temp');
    sessionStorage.setItem('just-a-placeholder', true);
  }

  const databases = await indexedDB.databases();
  console.log(databases.find(db => db.name === 'temp') !== undefined)
  await indexedDB.open('temp');
})();

Unfortunately, StackOverflow does not support running code snippets for demonstration purposes. However, you can check out this JSFiddle link to see it in action.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Activate the function only once the display has finished rendering all items from ng-repeat, not just when ng-repeat reaches its last index

Currently, I am generating a list using ng-repeat and each iteration is rendering a component tag with a unique id based on the $index value. The implementation looks like this: <div ng-if="$ctrl.myArr.length > 0" ng-repeat="obj in $ctrl.myArr"> ...

Loading jQuery on Ajax can be a challenge

I am currently faced with the challenge of working on code that I did not write myself or fully comprehend. The page utilizes AJAX to dynamically load content, and my goal is to manipulate this content. However, when I try to apply jQuery to the dynamic el ...

Error: Attempting to retrieve a refresh token from the Google API resulted in an Uncaught ReferenceError, as the

I am currently working on obtaining a refresh token once a user authorizes with Google in order to prevent the need for re-authorization. After studying Google's documentation, I learned that setting the access type to offline is necessary. Now, I am ...

Encoding URLs with PHP and JavaScript for enhanced security

I am currently working with a HTML text that was encoded in PHP using the urlencode function. Now, I need to decode this text in JavaScript. However, when I use the unescape function in JavaScript, all special characters are reverting back except for spa ...

Exploring the inner workings of self-referencing mechanics in functions

In a recent coding scenario, I encountered this situation: I attempted to define the func1() function and add several static methods to it simultaneously by passing it through the _init() function along with a hash containing properties to attach. However, ...

Generate a dynamic vertical line that glides smoothly over the webpage, gradually shifting from one end to the other within a set duration using javascript

I have designed a calendar timeline using html. My goal is to implement a vertical line that moves from left to right as time progresses, overlaying all other html elements. This functionality is similar to Google Calendar's way of indicating the curr ...

Accessing Elements by Class Name

I've been searching for information on the capabilities of getElementsByClassName for hours. While some sources mention it returns an HTML collection of items, length, and named items, w3schools provides an example where innerHTML is utilized: var x ...

What is the best approach to execute the jquery script exclusively on mobile devices?

I want to modify this code so that it only functions on mobile devices and is disabled on computers. How can I achieve this? <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body> <ul id="pri ...

Inspect the properties of a ReactJS component using Playwright

I'm relatively new to end-to-end (E2E) testing. One area I am looking to test involves changing the shipping address and automatically setting it as the billing address. For example, if I input Grove Street as my shipping address, I want it to mirror ...

Loop through each item using the .each() method, then make an AJAX

I need the "done." text to show up only after all the tasks within my each() function have completed. Despite attempting to use a delay, it seems that they are running asynchronously and the "done." message displays too early. Additionally, I am sending ...

Determine the presence of a JSON object within a file using JavaScript

Currently, I am developing a mobile app using react-native and have been facing challenges implementing error checking. To store data retrieved from an API in JSON format, I am employing redux along with thunk. At times, the search results yield a JSON res ...

What are some superior methods for determining the validity of a control in JavaScript?

Is there a way to determine the validity of a control in JavaScript within Asp.Net using a client-side API? Specifically, I am looking for a function that can check if a control is valid based on attached validators. For example, if a textbox has 2 validat ...

Difficulty with replacing colors in an image on certain devices when using HTML5 Canvas

I have created a 2d RTS HTML5 / Javascript game that utilizes images to represent the player's units and buildings. To achieve different variations of these images with different colors, I use a script that replaces certain colors in the original imag ...

The issue arises when trying to enqueue a Javascript file, causing an error stating that $ is not

I am having an issue with my WordPress plugin development where the jquery dependency is not being pulled in correctly, despite passing it in the third parameter of the wp_enqueue_scripts function. Upon inspecting with Google Chrome, I encountered the erro ...

Is it possible to add my own designs to a three.js model?

I am looking to add some personal touches to my three.js model by drawing on it in the scene. How can I achieve this effect of 'graffiti' on my models within the scene? ...

I'm having trouble with my bootstrap dropdown and I've exhausted all of my options trying to fix it based on my current understanding

My dropdown menu is not working despite adding all necessary Bootstrap CDN and files along with the jQuery script. Even with the table being handled by JavaScript, the button does not respond when clicked repeatedly. I suspect the issue lies within the han ...

Node.js Implementation of HTML Content

Currently, I am attempting to iterate through a list of items and generate HTML code to be passed into the view file: const itemWrap = '<div id="items"></div>'; userDetails.notes.forEach(item => { const itemE ...

Retrieving data from getServerSideProps and utilizing it inside Layout component in Next.js

Currently, I am in the process of developing a web application with Next.js. This project involves creating an admin dashboard that will be utilized to manage various tasks, each with its own SSR page. DashboardLayout : export const DashboardLayout = ({ch ...

Display all y-axis values in the tooltip using HighCharts

I utilized a chart example from http://www.highcharts.com/demo/column-stacked When you hover the mouse over a column, it displays the value of the y axis being hovered and the total value. I am interested in having the tooltip show the values of all 3 y ...

Execute the function with various parameters

I'm currently working on a form using HTML and JavaScript. One issue I'm facing is changing the focus when clicking on an input text field. I have created a function for this purpose, but I want to make it reusable for all input text fields, inst ...