国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Web Front-end CSS Tutorial HTML5 WebStorage (HTML5 local storage technology)_CSS/HTML

HTML5 WebStorage (HTML5 local storage technology)_CSS/HTML

May 16, 2016 pm 12:04 PM
html5

WebStorage is one of the local storage solutions in HTML5. Before the introduction of the WebStorage concept in HTML5, other unreliable solutions such as IE User Data, Flash Cookies, and Google Gears were removed. It was a browser-compatible local storage solution. Only using cookies. Some students may ask, since we have cookie local storage, why do we need to introduce the concept of WebStorage?

What’s wrong with Cookie

The flaws of cookies are very obvious

1. Data size: As a storage container, the size of cookie is limited to about 4KB, which is very annoying, especially for the current complex business logic requirements. In addition to storing some configuration fields, the 4KB capacity also stores simple single-value information. Most developers really don’t know what to expect.
2. Security issues: Since the cookie in the HTTP request is passed in clear text (HTTPS is not), the security issues are still huge.
3. Network burden: We know that cookies will be attached to each HTTP request and will be transmitted in the headers of HttpRequest and HttpResponse, so some unnecessary traffic losses will be added.

WebStorage

WebStorage is one of the new local storage solutions for HTML, but it is not a standard developed to replace cookies. Cookies are indispensable as part of the HTTP protocol to handle communication between the client and the server. Session is Implementation-dependent client-side state persistence. The purpose of WebStorage is to solve the problem of local storage that should not be done with cookies, but has to use cookies.
WebStorage provides two types of APIs: localStorage and sessionStorage. The difference between the two can be roughly understood by looking at the names. localStorage stores data locally permanently unless it is explicitly deleted or cleared. The data stored in sessionStorage is only in the session. It is valid for a certain period and will be automatically deleted when you close the browser. Both objects have a common API.

Copy code The code is as follows:

interface Storage {
readonly attribute unsigned long length ;
DOMString? key(unsigned long index);
getter DOMString getItem(DOMString key);
setter creator void setItem(DOMString key, DOMString value);
deleter void removeItem(DOMString key);
void clear();
};

1. Length: The only attribute, read-only, used to obtain the number of key-value pairs in the storage.
2. key: Get the key name of the storage based on the index
3. getItem: get the corresponding value in the storage based on the key
4. setItem: add a key-value pair to the storage
5. removeItem: Delete the key-value pair according to the key name
6. clear: clear the storage object

How to use WebStorage

In a browser that implements WebStorage, the page has two global objects, localStorage and sessionStorage
HTML5 WebStorage (HTML5 local storage technology)_CSS/HTML
Take localStorage as an example and look at a simple operation code

Copy code The code is as follows:

var ls=localStorage;
??????????? console.log(ls.length);//0
??????????? ls.setItem('name','Byron');
??????????? ls.setItem('age','24');
??????????? console.log(ls.length);//2

??????????? //遍歷localStorage
??????????? for(var i=0;i??????????????? /*
??????????????????? age : 24
??????????????????? name : Byron
??????????????? */
??????????????? var key=ls.key(i);
??????????????? console.log(key ' : ' ls.getItem(key));
??????????? }

??????????? ls.removeItem('age');

???????????
??????????? for(var i=0;i??????????????? /*
??????????????????? name : Byron
??????????????? */
??????????????? var key=ls.key(i);
??????????????? console.log(key ' : ' ls.getItem(key));
??????????? }
??????????? ls.clear();//0
??????????? console.log(ls.length);

事件

同時(shí)HTML5規(guī)定了一個(gè)storage事件,在WebStorage發(fā)生變化的時(shí)候觸發(fā),可以用此監(jiān)視不同頁(yè)面對(duì)storage的修改

復(fù)制代碼 代碼如下:

interface StorageEvent: Event {
readonly attribute DOMString key;
readonly attribute DOMString? oldValue;
readonly attribute DOMString? newValue;
readonly attribute DOMString url;
readonly attribute Storage? storageArea;
};

1. key: the key of the key-value pair
2. oldValue: the value before modification3. newValue: the modified value
4. url: the page url that triggered the change
5. StorageArea: the changed Storage

Defined in index.php

Copy code The code is as follows:

Copy code The code is as follows:

window.addEventListener('storage',function(e ; BR>?????},false);
??????? localStorage.setItem('userName','Byron');

test.php

Copy code The code is as follows:

localStorage.setItem(' userName','Casper');

When you click the link on the index.php page to access test.php, you can see the console output log of index.php:
userName is changed form Byron to Casper by http://localhost/test.php
true

Why it is better than cookies

1. In terms of capacity, WebStorage generally provides 5M of storage space in browsers, which is not enough to store videos and pictures, but it is sufficient for most operations
2. In terms of security, WebStorage does not play a role. HTTP header is sent by the browser, so it is relatively safe
3. In terms of traffic, because WebStorage is not transmitted to the server, unnecessary traffic can be saved, which is still very convenient for high-frequency visits or web pages targeting mobile devices. Not bad.
This does not mean that WebStorage can replace cookies, but that with WebStorage, cookies can only do what it should do - serve as a channel for interaction between the client and the server, and maintain the client state. So WebStorage is superior to cookies just as a local storage solution.

Things to note

1. Browser compatibility, this is almost the easiest to implement among all new HTML5 features, because all IE8 browsers support it. IE7 and IE6 can be implemented using IE User Data.

HTML5 WebStorage (HTML5 local storage technology)_CSS/HTML
2. Since localStorage and sessionStorage are both objects, you can also obtain and modify key-value pairs through ".key" or "[key]", but this is not recommended.
Copy code The code is as follows:

localStorage.userName='Frank';
console.log(localStorage['userName']);

3. Although localStorage is stored locally, different browsers store data independently, so the localStorage stored on Chrome is It is not available on FireFox.
4. localStorage and sessionStorage can only store string types. For complex objects, you can use stringify and parse of the JSON object provided by ECMAScript. For lower versions of IE, you can use json2.js
5. In addition to the console, Chrome also provides a very intuitive display method for local storage, which is very convenient when debugging
HTML5 WebStorage (HTML5 local storage technology)_CSS/HTML
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

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.

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)

Is H5 a Shorthand for HTML5? Exploring the Details Is H5 a Shorthand for HTML5? Exploring the Details Apr 14, 2025 am 12:05 AM

H5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.

H5 and HTML5: Commonly Used Terms in Web Development H5 and HTML5: Commonly Used Terms in Web Development Apr 13, 2025 am 12:01 AM

H5 and HTML5 refer to the same thing, namely HTML5. HTML5 is the fifth version of HTML, bringing new features such as semantic tags, multimedia support, canvas and graphics, offline storage and local storage, improving the expressiveness and interactivity of web pages.

Understanding H5 Code: The Fundamentals of HTML5 Understanding H5 Code: The Fundamentals of HTML5 Apr 17, 2025 am 12:08 AM

HTML5 is a key technology for building modern web pages, providing many new elements and features. 1. HTML5 introduces semantic elements such as, , etc., which enhances web page structure and SEO. 2. Support multimedia elements and embed media without plug-ins. 3. Forms enhance new input types and verification properties, simplifying the verification process. 4. Offer offline and local storage functions to improve web page performance and user experience.

HTML5: The Building Blocks of the Modern Web (H5) HTML5: The Building Blocks of the Modern Web (H5) Apr 21, 2025 am 12:05 AM

HTML5 is the latest version of the Hypertext Markup Language, standardized by W3C. HTML5 introduces new semantic tags, multimedia support and form enhancements, improving web structure, user experience and SEO effects. HTML5 introduces new semantic tags, such as, ,, etc., to make the web page structure clearer and the SEO effect better. HTML5 supports multimedia elements and no third-party plug-ins are required, improving user experience and loading speed. HTML5 enhances form functions and introduces new input types such as, etc., which improves user experience and form verification efficiency.

HTML5: The Standard and its Impact on Web Development HTML5: The Standard and its Impact on Web Development Apr 27, 2025 am 12:12 AM

The core features of HTML5 include semantic tags, multimedia support, offline storage and local storage, and form enhancement. 1. Semantic tags such as, etc. to improve code readability and SEO effect. 2. Simplify multimedia embedding with labels. 3. Offline storage and local storage such as ApplicationCache and LocalStorage support network-free operation and data storage. 4. Form enhancement introduces new input types and verification properties to simplify processing and verification.

HTML5 and H5: Understanding the Common Usage HTML5 and H5: Understanding the Common Usage Apr 22, 2025 am 12:01 AM

There is no difference between HTML5 and H5, which is the abbreviation of HTML5. 1.HTML5 is the fifth version of HTML, which enhances the multimedia and interactive functions of web pages. 2.H5 is often used to refer to HTML5-based mobile web pages or applications, and is suitable for various mobile devices.

The Connection Between H5 and HTML5: Similarities and Differences The Connection Between H5 and HTML5: Similarities and Differences Apr 24, 2025 am 12:01 AM

H5 and HTML5 are different concepts: HTML5 is a version of HTML, containing new elements and APIs; H5 is a mobile application development framework based on HTML5. HTML5 parses and renders code through the browser, while H5 applications need to run containers and interact with native code through JavaScript.

Understanding H5: The Meaning and Significance Understanding H5: The Meaning and Significance May 11, 2025 am 12:19 AM

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.

See all articles