localStorage not updating unless you refresh the page

Hello,

I know this is not F7 related but it is widely used in F7.
everything went well setting and getting value from localstorage
but i noticed. the localstorage value is not
really updating unless i refresh the page.

how to resolve this?
thank you!

Please provide a code sample, so we can see where you might be going wrong.

window.localStorage.getItem(“atoken”) ----- window. → localStorage

Well, i can’t show the whole codes as they are already complex and hard to explain.

but ill show how i do it…

//let’s this is the html where the localstorage value should appear
[div id=“sample-id”][/div] (i realized html tags dont appear here)

// app.js
let eid= localStorage.getItem(“eid”); // get the item
$(’#sample-id’).html(eid); // wont appear for the first load
console.log(eid) // but the eid value appears on console.

even i put the codes inside the

  • function samplefunc(){…
    -$(’#tabbar-main .tab-link-play’).on(‘click’, function() { …
  • or page init…

it does the same thing…

should i add “window.” ? ill try that.


btw, i use sessionStorage instead of localStorage because i just need the browser or app to hold temporary data during user’s browsing… like event_id ← to know which event they are viewing.
localStorage is often used in login feature.

This is how we do it in f7 may be it will helpful for you

//let’s this is the html where the localstorage value should appear
// change here
${loaded && [div id=“sample-id”][/div] }(i realized html tags dont appear here)

// app.js
// change here
let loaded = false;
let eid= localStorage.getItem(“eid”); // get the item
// change here
if (eid){
loaded = true;
$update();
}
$(’#sample-id’).html(eid); // wont appear for the first load
console.log(eid) // but the eid value appears on console.

even i put the codes inside the

  • function samplefunc(){…
    -$(’#tabbar-main .tab-link-play’).on(‘click’, function() { …
  • or page init…

Is the value that you’ve saved in localStorage a plain text value, or an HTML-encoded string? You could just another jQuery modifier other than ‘HTML’ to render it, such as ‘text’. You can also use Framework7’s Dom7 to manipulate the DOM this way.

i tried $().text(); and it worked. i tried html();
to my surprised, it also worked.

last time, it didnt work. i didnt understand.
but it works now. thank you!

I actually got the logic…
but this ${loaded && ← thing is giving me error.
i think this is placed with

all my codes are in app.js
not inside the pages/whateverpage.html where there is export…
im still using the old-school js procedural method
but thank you for the effort… ill keep this as future reference :smiley: