Table of Contents
Preface
Usage
setItem
getItem
removeItem
clear" >clear
key
Constructor" >Constructor
Events" >Listen to storage Events
Debugging
Compatible
Home Web Front-end H5 Tutorial HTML5 Advanced Tutorial - Web Storage

HTML5 Advanced Tutorial - Web Storage

May 08, 2017 pm 01:46 PM

Preface

HTML5 There are two web Storage storage methods: localStorage and sessionStorage.

Both methods save data through key-value pairs, which are easy to access and do not affect website performance. Their usage is the same, but their storage times are different.
LocalStorage data is saved on local hardware and can be saved permanently. The api can be manually called to clear the data. sessionStorage is stored in the session object and will be cleared when the browser is closed.

The size of web Storage is limited on the browser. The size of different browsers will be different. In mainstream browsers, the size is about 5M, which is actually enough to store ordinary data.

Usage

Take localStorage as an example, the usage of sessionStorage is the same:

setItem

Save data: localStorage.setItem(key,value);

Example:

localStorage.setItem('name','Hello World');
Copy after login

When the keys are the same, the previous value will be overwritten to modify the data. If value is an object, it needs to be converted to a json string, otherwise what you read will be [object Object]

getItem

Read data: localStorage.getItem(key);

Example:

localStorage.getItem('name');       // Hello World
Copy after login

removeItem

Remove Single data: localStorage.removeItem(key);

Example:

localStorage.removeItem('name');
localStorage.getItem('name');       // null
Copy after login

After deleting the data with key name, if the data cannot be obtained in loaclStorage, null will be returned;

clear

Delete all data: localStorage.clear ();

Example:

localStorage.clear();
Copy after login

All data in localStorage will be deleted at this time.

key

Get the key of a index: localStorage.key(index);
Example:

localStorage.setItem('name1','Hello World');
localStorage.setItem('name2','Hello Linxin');
localStorage.key(1);                // name2
Copy after login

Get the index as 1 The key is name2.

Constructor

In actual projects, localStorage may need to be operated multiple times. We can operate it better through a constructor.

Example:

var localEvent = function (item) {
    this.get = function () {
        return localStorage.getItem(item);
    }
    this.set = function (val) {
        localStorage.setItem(item, val);
    }
    this.remove = function () {
        localStorage.removeItem(item);
    }
    this.clear = function () {
        localStorage.clear();
    }
}

// 使用new字符把构造函数实例化出多个对象
var local1 = new localEvent('name1');
var local2 = new localEvent('name2');

local1.set('Hello World');
local2.set('Hello Linxin');

local1.get();               // Hello World
local2.get();               // Hello Linxin
Copy after login

This is just a simple demonstration. If we usually store objects in our projects, we need to do some processing in the code.

Listen to storage Events

You can listen to the storage event of the window object and specify its Event processing function, when localStorage or sessionStorage is processed in the page When modified, the corresponding processing function will be triggered.

window.addEventListener('storage',function(e){
    console.log('key='+e.key+',oldValue='+e.oldValue+',newValue='+e.newValue);
})
Copy after login

The time object (e parameter value) that triggers the event has several attributes:

  • key: key value.

  • oldValue: The value before modification.

  • newValue: The modified value.

  • url: Page url.

  • storageArea: The modified storage object.

Note: In Google Chrome, the storage needs to be modified in different tabs to trigger this event, that is, web page A listens to this event, and localStorage is modified in web page B, then the web page A will trigger the event function. But in IE, modifying localStorage on the same web page will trigger this event.

Debugging

Google Chrome’s own debugging tools (chrome devtools) are very easy to use and can be used to debug localStorage and sessionStorage. Open the browser and press f12 to bring up the debugging tool. You can see Application. Click to open and you can see Storage in the left column, including localStorage, sessionStorage, IndexedDB, etc. Select the domain name of the website we want to debug, and you can see the corresponding key on the right. and value, which can be edited or deleted by right-clicking.

Compatible

It is compatible with IE8 and above, but it is special and only supports it when it needs to be opened on the server. Directly double-clicking file:// to open the file is incompatible.

Only IE11 supports opening under file://. Other browsers have a high degree of support, including compatibility on mobile phones.

【Related recommendations】

1. Free h5 online video tutorial

2. HTML5 full version manual

3. php.cn original html5 video tutorial

The above is the detailed content of HTML5 Advanced Tutorial - Web 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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.

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.

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 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