Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

localStorage.removeItem not working properly . How can i fix it?

I have this code for saving data from 2 inputs. Everything is all right when the page refreshes twice (the data is saved) but on third one the date disappear. I know this is because of the localStorage.removeItem, but I need the data to disappear after the tab is gone.

I use the same method for other stuff and it's working perfect. but in this case it's how I said , working for just 2 refreshes .

<input type="date" id="exdated" name="" 
        onfocus="localStorage.setItem('myTestApp','exdated')" 
        onkeyup="saveValue(this);">
<br><br> 
<input type="date" id="datata" name="" 
        onfocus="localStorage.setItem('myTestApp','datata')" 
        onkeyup="saveValue(this);">
<br><br>
let today = new Date().toISOString().slice(0, 10)

document.getElementById("datata").value = getSavedValue("datata"); 
document.getElementById("exdated").value = getSavedValue("exdated"); 

function saveValue(e) {
    var id = e.id;  
    var val = e.value; 
 
    localStorage.setItem(id, val); 
}

function getSavedValue  (v) {
    if (!localStorage.getItem(v)) {
      return today;
    }

    var itemcheta = localStorage.getItem(v);
    localStorage.removeItem(v); 
    
    return itemcheta;
}
like image 309
stoperan Avatar asked Sep 01 '21 11:09

stoperan


People also ask

Why doesn't localStorage use savevalue()?

you never use saveValue () so your localeStorage is empty and localStorage.removeItem (v) is never called. Please include a complete minimal reproducible example. Why don't you use sessionStorage instead of localStorage?

What is the difference between getitem/setitem and object-like access in localStorage?

If the key is user-generated, it can be anything, like length or toString, or another built-in method of localStorage. In that case getItem/setItem work fine, while object-like access fails: There’s a storage event, it triggers when we modify the data. That event does not happen for object-like access. We’ll see that later in this chapter.

What is localStorage and how to use it?

The main features of localStorage are: Shared between all tabs and windows from the same origin. The data does not expire. It remains after the browser restart and even OS reboot. For instance, if you run this code… …And close/open the browser or just open the same page in a different window, then you can get it like this:

How do I remove an item from a storage object?

The removeItem () method belongs to the Storage Object, which can be either a localStorage object or a sessionStorrage object. Required. A String specifying the name of the item you want to remove The same example, but using session storage instead of local storage.


1 Answers

Since you want the data to disappear when the browser tab closes, you should use sessionStorage instead of localStorage. This will let the browser handle the correct removal of the data when the browsing session closes, so it's less code to worry about.

like image 70
Alex Gyoshev Avatar answered Sep 24 '22 17:09

Alex Gyoshev