🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to HTML Notes
Topic #76

HTML Web Storage


HTML Web Storage API; better than cookies.


What is HTML Web Storage?

With web storage, applications can store data locally within the user's browser.

Before HTML5, application data had to be stored in cookies, included in every server request. Web storage is more secure, and large amounts of data can be stored locally, without affecting website performance.

Unlike cookies, the storage limit is far larger (at least 5MB) and information is never transferred to the server.

Web storage is per origin (per domain and protocol). All pages, from one origin, can store and access the same data.


Web Storage API Objects

Web storage provides two objects for storing data in the browser:

  • window.localStorage - stores data with no expiration date (data is not lost when the browser tab is closed)
  • window.sessionStorage - stores data for one session (data is lost when the browser tab is closed)

Browser Support

The numbers in the table specify the first browser version that fully supports Web Storage.


Test Web Storage API Support

Before using web storage, we can quickly check browser support for localStorage and sessionStorage:

Example

  <script>
const x = document.getElementById("result");
if (typeof(Storage)
  !== "undefined") {
  x.innerHTML = "Your browser supports Web
  storage!";
} else {
  x.innerHTML = "Sorry, no Web storage
  support!";
}
</script>

The localStorage Object

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

Example

  <script>
const x = document.getElementById("result");

if (typeof(Storage)
  !== "undefined") {
  // Store
  localStorage.setItem("lastname",
  "Smith");
  localStorage.setItem("bgcolor", "yellow");
  //
  Retrieve
  x.innerHTML = localStorage.getItem("lastname");

  x.style.backgroundColor = localStorage.getItem("bgcolor");
} else {

  x.innerHTML = "Sorry, no Web storage support!";
}
</script>

Example explained:

  • Use the localStorage.setItem() method to create name/value pairs
  • Use the localStorage.getItem() method to retrieve the values set
  • Retrieve the value of "lastname" and insert it into an element with id="result"
  • Retrieve the value of "bgcolor" and insert it into the style backgroundColor of the element with id="result"

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

localStorage.removeItem("lastname");

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


Counting Clicks with localStorage

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:

Example

  <script>
function clickCounter() {
  const x =
  document.getElementById("result");
  if (typeof(Storage) !==
  "undefined") {
    if (localStorage.clickcount) {

  localStorage.clickcount = Number(localStorage.clickcount)+1;

  } else {
      localStorage.clickcount = 1;

  }
    x.innerHTML = "You have clicked the button " +
  localStorage.clickcount + " time(s)!";
  } else {

  x.innerHTML = "Sorry, no Web storage support!";
  }
}
</script>

The sessionStorage Object

The sessionStorage object is equal to the localStorage object, except that it stores the data for only one session! The data is deleted when the user closes the specific browser tab.

Counting Clicks with sessionStorage

The following example counts the number of times a user has clicked a button, in the current session:

Example

  <script>
function clickCounter() {
  const x =
  document.getElementById("result");
  if (typeof(Storage) !==
  "undefined") {
    if (sessionStorage.clickcount) {

  sessionStorage.clickcount = Number(sessionStorage.clickcount)+1;

  } else {
      sessionStorage.clickcount = 1;

  }
    x.innerHTML = "You have clicked the button " +
  sessionStorage.clickcount + " time(s) in this session!";
  } else {

  x.innerHTML = "Sorry, no Web storage support!";
  }
}
</script>

Want to go beyond the notes?

Join CodingNow 2.0's HTML course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

HTML Web Storage – FAQs

Quick answers about learning HTML Web Storage in HTML.

This free note from CodingNow 2.0 explains HTML Web Storage in HTML — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every HTML topic on CodingNow 2.0, including HTML Web Storage, is 100% free with no signup required.
With focused practice, most students grasp HTML Web Storage in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now