Horje
How to create HTML localStorage Object | HTML Web Storage API Code
How to create HTML localStorage Object

The localStorage object stores the data with no expiration date. The data will not be deleted when the browser is closed, and will be available the next day, week, or year.


Learn localStorage Object

<script>
// Store
localStorage.setItem("lastname", "Smith");

// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("lastname");
</script>

Output should be:

Learn localStorage Object

Example explained: localStorage Object

The example above could also be written like this:

// Store
localStorage.lastname = "Smith";
// Retrieve
document.getElementById("result").innerHTML = localStorage.lastname;

Output should be:

The example above could also be written like this:

The syntax for removing the 'lastname' localStorage item is as follows:

<script>
 localStorage.removeItem("lastname");
</script>

Note: Name/value pairs are always stored as strings. Remember to convert them to another format when needed!

The following example counts the number of times a user has clicked a button. In this code the value string is converted to a number to be able to increase the counter:

<script>
if (localStorage.clickcount) {
  localStorage.clickcount = Number(localStorage.clickcount) + 1;
} else {
  localStorage.clickcount = 1;
}
document.getElementById("result").innerHTML = "You have clicked the button " +
localStorage.clickcount + " time(s)."; 
</script>

Output should be:

Reffered: https://www.w3schools.com/html/html5_webstorage.asp



Read Full::
HTML Web Storage API Code
Category:
HTML Code
Sub Category:
HTML Web Storage API Code
Uploaded:
1 month ago
Uploaded by:
Admin
Views:
2

Related Articles
What is HTML Web Storage API HTML Web Storage API Code
What is HTML Web Storage? HTML Web Storage API Code
What Browsers will Support HTML Web Storage API HTML Web Storage API Code
How to create HTML Web Storage Objects HTML Web Storage API Code
How to create HTML localStorage Object HTML Web Storage API Code
How to create HTML sessionStorage Object HTML Web Storage API Code

Single Articles
Learn localStorage Object
Example explained: localStorage Object
The example above could also be written like this:
The syntax for removing the 'lastname' localStorage item is as follows:

Share on: