How to prevent data from disappearing on webpage

Viewed 29

I want to prevent my data from disappearing on the webpage whenever I refresh the page. Please how do I go about that with local storage in JavaScript?

2 Answers

I would like to suggest resources like https://www.w3schools.com/

but take a look at this

<p>Saved name is:</p>
<p id="demo"></p>

<script>
// Set Item
localStorage.setItem("lastname", "Smith");
// Retrieve
document.getElementById("demo").innerHTML = localStorage.getItem("lastname");
</script>

Let's say if you want the user to login after he has logged in the first time on your website then you will use localStorage. How to use it?

// to add new item to the cache memory of the user
localStorage.setItem('key', //anything here);
//to get it
localStorage.getItem('key');
// to remove it
localStorage.removeItem('key');
// to remove all of it
localStorage.clear();


Related