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.
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
Take localStorage as an example and look at a simple operation code
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);
事件
同时HTML5规定了一个storage事件,在WebStorage发生变化的时候触发,可以用此监视不同页面对storage的修改
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
window.addEventListener('storage',function(e ; BR> },false);
localStorage.setItem('userName','Byron');
test.php
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.
![]() |
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的div元素默认一行不可以放两个。div是一个块级元素,一个元素会独占一行,两个div默认无法在同一行显示;但可以通过给div元素添加“display:inline;”样式,将其转为行内元素,就可以实现多个div在同一行显示了。

html5中列表和表格的区别:1、表格主要是用于显示数据的,而列表主要是用于给数据进行布局;2、表格是使用table标签配合tr、td、th等标签进行定义的,列表是利用li标签配合ol、ul等标签进行定义的。

固定方法:1、使用header标签定义文档头部内容,并添加“position:fixed;top:0;”样式让其固定不动;2、使用footer标签定义尾部内容,并添加“position: fixed;bottom: 0;”样式让其固定不动。

HTML5中画布标签是“<canvas>”。canvas标签用于图形的绘制,它只是一个矩形的图形容器,绘制图形必须通过脚本(通常是JavaScript)来完成;开发者可利用多种js方法来在canvas中绘制路径、盒、圆、字符以及添加图像等。

html5中不支持的标签有:1、acronym,用于定义首字母缩写,可用abbr替代;2、basefont,可利用css样式替代;3、applet,可用object替代;4、dir,定义目录列表,可用ul替代;5、big,定义大号文本等等。

html5废弃了dir列表标签。dir标签被用来定义目录列表,一般和li标签配合使用,在dir标签对中通过li标签来设置列表项,语法“<dir><li>列表项值</li>...</dir>”。HTML5已经不支持dir,可使用ul标签取代。

html5是指超文本标记语言(HTML)的第五次重大修改,即第5代HTML。HTML5是Web中核心语言HTML的规范,用户使用任何手段进行网页浏览时看到的内容原本都是HTML格式的,在浏览器中通过一些技术处理将其转换成为了可识别的信息。HTML5由不同的技术构成,其在互联网中得到了非常广泛的应用,提供更多增强网络应用的标准机。

3种取消方法:1、给td元素添加“border:none”无边框样式即可,语法“td{border:none}”。2、给td元素添加“border:0”样式,语法“td{border:0;}”,将td边框的宽度设置为0即可。3、给td元素添加“border:transparent”样式,语法“td{border:transparent;}”,将td边框的颜色设置为透明即可。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor
