Table of Contents
1. Overview
2. Operation method
2.1 Saving/reading data
2.2 Clear data
2.3 Traversal operation
Event" >3. storageEvent
Home Web Front-end H5 Tutorial Specific analysis of localStorage and sessionStorage of HTML5 local storage

Specific analysis of localStorage and sessionStorage of HTML5 local storage

Mar 30, 2017 pm 01:15 PM

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;

}
Copy after login

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');
Copy after login

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');
Copy after login

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');
Copy after login

clear method is used to clear all saved data.

sessionStorage.clear();

localStorage.clear();
Copy after login

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));
}
Copy after login

The key method obtains the key value based on the position (starting from 0).

localStorage.key(1);
Copy after login

3. storageEvent

When the stored data changes, the storage event will be triggered. We can specify the callback function for this event.

window.addEventListener(&#39;storage&#39;, onStorageChange);
Copy after login

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);
}
Copy after login

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!

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

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

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)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles