Web Storage: Local and Session Storage

Introduction to Web Storage API

In the early days of web development, cookies were the only way to store data on the client side. However, they had limitations such as small storage capacity and the fact that they were sent with every HTTP request, potentially impacting performance.

With the introduction of HTML5, developers gained access to the Web Storage API, which provides two powerful tools for client-side storage: sessionStorage and localStorage. These tools are simpler to use, offer more storage space, and do not involve server communication unless explicitly programmed to do so.

Session Storage

sessionStorage is like a sticky note that lasts only until you close your browser tab. It is ideal for temporary data that doesn’t need to persist between browser sessions.

Features and Benefits:

Here’s an example of using sessionStorage to autosave the contents of a text field and restore it if the browser is refreshed:


// Get the text field element
let field = document.getElementById("field");

// Check if sessionStorage contains an autosave value
if (sessionStorage.getItem("autosave")) {
  // Restore the text field's content
  field.value = sessionStorage.getItem("autosave");
}

// Save the text field's content to sessionStorage on change
field.addEventListener("change", function () {
  sessionStorage.setItem("autosave", field.value);
});
            

Analogy: Imagine jotting down notes on a whiteboard during a meeting. The notes are helpful while the meeting is ongoing (the session), but they’re erased when the meeting ends (the tab is closed).

Local Storage

localStorage is a more permanent option, designed for data that should persist even after the browser is closed. It’s like saving notes to a personal notebook that you can access anytime you return.

Features and Benefits:

Here’s an example of using localStorage to save and apply page styles:


// Check if localStorage has saved styles
if (!localStorage.getItem("bgcolor")) {
  populateStorage();
}
setStyles();

function populateStorage() {
  localStorage.setItem("bgcolor", document.getElementById("bgcolor").value);
  localStorage.setItem("font", document.getElementById("font").value);
  localStorage.setItem("image", document.getElementById("image").value);
}

function setStyles() {
  const currentColor = localStorage.getItem("bgcolor");
  const currentFont = localStorage.getItem("font");
  const currentImage = localStorage.getItem("image");

  document.getElementById("bgcolor").value = currentColor;
  document.getElementById("font").value = currentFont;
  document.getElementById("image").value = currentImage;

  document.documentElement.style.backgroundColor = `#${currentColor}`;
  document.querySelector("p").style.fontFamily = currentFont;
  document.querySelector("img").src = currentImage;
}
            

Viewing and Managing Web Storage

You can inspect and manage data stored in localStorage and sessionStorage using your browser's developer tools:

  1. Open the Developer Tools (F12 or Ctrl+Shift+I).
  2. Go to the "Application" tab in Chrome or the "Storage" tab in Firefox.
  3. Under "Storage," you’ll see options for Local Storage and Session Storage.
  4. Select a specific site to view its stored keys and values.
  5. To delete a key-value pair, highlight it and press the Delete or Backspace key, or right-click and select "Delete."

Key Takeaways

By mastering the Web Storage API, you can create seamless, personalized, and efficient user experiences in your web applications.