search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

Table of Contents
1. Save Data with localStorage.setItem()
2. Retrieve Data with localStorage.getItem()
3. Remove Data with localStorage.removeItem()
4. Clear All Data with localStorage.clear()
5. Check Available Keys
Important Notes
Home Web Front-end H5 Tutorial How do you use local storage in HTML5?

How do you use local storage in HTML5?

Aug 17, 2025 pm 12:09 PM

Local storage in HTML5 enables persistent client-side data storage as key-value pairs. 1. Use localStorage.setItem('key', 'value') to save strings; for objects, use JSON.stringify(). 2. Retrieve data with localStorage.getItem('key') and parse objects using JSON.parse(). 3. Remove specific items via localStorage.removeItem('key'). 4. Clear all data with localStorage.clear(). 5. Loop through keys using localStorage.length and localStorage.key(i) to access each key-value pair. Storage is limited to 5–10 MB per domain, follows same-origin policy, is synchronous, has no expiration, and only supports string values, requiring JSON methods for objects; always handle cases where storage may be full or disabled.

How do you use local storage in HTML5?

Local storage in HTML5 allows you to store data in the user's browser that persists even after the browser is closed. It's useful for saving user preferences, form data, or any information you want to keep between sessions without sending it to the server.

How do you use local storage in HTML5?

Here’s how to use it:

1. Save Data with localStorage.setItem()

To store data, use the setItem() method. Both the key and value must be strings.

How do you use local storage in HTML5?
localStorage.setItem('username', 'john_doe');
localStorage.setItem('theme', 'dark');

If you need to store an object or array, convert it to a JSON string first:

const user = { name: 'John', age: 30 };
localStorage.setItem('user', JSON.stringify(user));

2. Retrieve Data with localStorage.getItem()

Use getItem() to read data by its key. It returns null if the key doesn’t exist.

How do you use local storage in HTML5?
const username = localStorage.getItem('username'); // returns 'john_doe'
const user = JSON.parse(localStorage.getItem('user')); // convert back to object

3. Remove Data with localStorage.removeItem()

Delete a specific item by key:

localStorage.removeItem('username');

4. Clear All Data with localStorage.clear()

Removes all stored data for that domain:

localStorage.clear(); // removes everything

5. Check Available Keys

You can loop through all stored keys using localStorage.length and localStorage.key():

for (let i = 0; i < localStorage.length; i  ) {
  const key = localStorage.key(i);
  const value = localStorage.getItem(key);
  console.log(key, value);
}

Important Notes

  • Storage Limit: Usually around 5–10 MB per domain.
  • Same-Origin Policy: Data is accessible only to pages from the same origin (protocol, domain, and port).
  • Synchronous: Operations block the main thread, so avoid storing very large amounts of data.
  • No Expiration: Unlike cookies, data doesn’t expire unless manually removed.

You can’t store functions, DOM nodes, or complex objects directly—only strings. Always use JSON.stringify() and JSON.parse() for objects and arrays.

Basically, it’s a simple key-value store built into modern browsers. Just remember to handle cases where storage might be full or disabled.

The above is the detailed content of How do you use local storage in HTML5?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Popular tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)