


1. Overview
localStorage and sessionStorage are collectively called Web Storage, which allows web pages to store data on the browser side.
The data saved by sessionStorage is used for a browser session. When the session ends (usually the window is closed), the data is cleared; the data saved by localStorage exists for a long time. The next time you visit the website, the web page can Read previously saved data directly. Except for the different storage periods, the properties and methods of these two objects are exactly the same.
They are very much like an enhanced version of the cookie mechanism and can use much larger storage space. Currently, the storage limit per domain depends on the browser, and is 2.5MB for Chrome, 5MB for Firefox and Opera, and 10MB for IE. Among them, Firefox's storage space is determined by the first-level domain name, while other browsers do not have this limitation. That is, in Firefox, a.example.com and b.example share 5MB of storage space. In addition, like cookies, they are also subject to same domain restrictions. The data stored in a web page can only be read by web pages in the same domain.
By checking whether the window object contains the sessionStorage and localStorage properties, you can determine whether the browser supports these two objects.
function checkStorageSupport() { var result = {}; //sessionStorage if (window.sessionStorage) { result.sessionStorage = true; } else { result.sessionStorage = false; } //localStorage if (window.localStorage) { result.localStorage = true; } else { result.localStorage = false; } return result; }
2. Operation method
2.1 Saving/reading data
The data saved by sessionStorage and localStorage exist in the form of "key-value pairs". In other words, each item of data has a key name and a corresponding value. All data are saved in text format.
Use the setItem method to store data. It accepts two parameters, the first is the key name and the second is the saved data.
sessionStorage.setItem('key', 'value'); localStorage.setTime('key', 'value');
Read data using the getItem method. It has only one parameter, which is the key name.
var valueSession = sessionStorage.getItem('key');var valueLocal = localStorage.getItem('key');
2.2 Clear data
The removeItem method is used to clear the data corresponding to a certain key name.
sessionStorage.removeItem('key'); localStorage.removeItem('key');
clear method is used to clear all saved data.
sessionStorage.clear(); localStorage.clear();
2.3 Traversal operation
Using the length attribute and key method, all keys can be traversed.
for (var i = 0; i < localStorage.length; i++) { console.log(localStorage.key(i)); }
The key method obtains the key value based on the position (starting from 0).
localStorage.key(1);
3. storageEvent
When the stored data changes, the storage event will be triggered. We can specify the callback function for this event.
window.addEventListener('storage', onStorageChange);
The callback function accepts an event object as a parameter. The key attribute of this event object saves the changed key name.
function onStorageChange(e) { console.log(e.key); }
In addition to the key attribute, the event object has three attributes:
oldValue: the value before update. If the key is newly added, this attribute is null.
newValue: updated value. If the key was deleted, this property is null.
url: The URL of the web page that originally triggered the storage event.
It is worth noting that this event is not triggered on the current page that causes the data to change. If the browser opens multiple pages under a domain name at the same time, when one of the pages changes the data of sessionStorage or localStorage, the storage events of all other pages will be triggered, and the storage event will not be triggered by the original page. Communication between multiple windows can be achieved through this mechanism. Among all browsers, except IE, it will trigger the storage event on all pages.
The above is the detailed content of Specific analysis of localStorage and sessionStorage of HTML5 local storage. For more information, please follow other related articles on the PHP Chinese website!

H5 is HTML5, the fifth version of HTML. HTML5 improves the expressiveness and interactivity of web pages, introduces new features such as semantic tags, multimedia support, offline storage and Canvas drawing, and promotes the development of Web technology.

Accessibility and compliance with network standards are essential to the website. 1) Accessibility ensures that all users have equal access to the website, 2) Network standards follow to improve accessibility and consistency of the website, 3) Accessibility requires the use of semantic HTML, keyboard navigation, color contrast and alternative text, 4) Following these principles is not only a moral and legal requirement, but also amplifying user base.

The H5 tag in HTML is a fifth-level title that is used to tag smaller titles or sub-titles. 1) The H5 tag helps refine content hierarchy and improve readability and SEO. 2) Combined with CSS, you can customize the style to enhance the visual effect. 3) Use H5 tags reasonably to avoid abuse and ensure the logical content structure.

The methods of building a website in HTML5 include: 1. Use semantic tags to define the web page structure, such as, , etc.; 2. Embed multimedia content, use and tags; 3. Apply advanced functions such as form verification and local storage. Through these steps, you can create a modern web page with clear structure and rich features.

A reasonable H5 code structure allows the page to stand out among a lot of content. 1) Use semantic labels such as, etc. to organize content to make the structure clear. 2) Control the rendering effect of pages on different devices through CSS layout such as Flexbox or Grid. 3) Implement responsive design to ensure that the page adapts to different screen sizes.

The main differences between HTML5 (H5) and older versions of HTML include: 1) H5 introduces semantic tags, 2) supports multimedia content, and 3) provides offline storage functions. H5 enhances the functionality and expressiveness of web pages through new tags and APIs, such as and tags, improving user experience and SEO effects, but need to pay attention to compatibility issues.

The difference between H5 and HTML5 is: 1) HTML5 is a web page standard that defines structure and content; 2) H5 is a mobile web application based on HTML5, suitable for rapid development and marketing.

The core features of HTML5 include semantic tags, multimedia support, form enhancement, offline storage and local storage. 1. Semantic tags such as, improve code readability and SEO effect. 2. Multimedia support simplifies the process of embedding media content through and tags. 3. Form Enhancement introduces new input types and verification properties, simplifying form development. 4. Offline storage and local storage improve web page performance and user experience through ApplicationCache and localStorage.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
