Posts in current category
May 03, 2021 13:10 HTML5
Prior to HTML5, cookies were mainly used for storage, and the disadvantages of cookies were: data needed to be carried on the request head, storage size, however, within 4k. In this section,
HTML5 web storage, a better local storage method than cookies.
HTML5 allows you to store the user's browsing data locally.
Earlier, local storage used cookies. B ut Web storage needs to be more secure and fast. T his data is not stored on the server, but it is only used on site data requested by the user. It can also store large amounts of data without affecting the performance of the site.
The data exists as a key/value pair, and the data of the web page is only allowed to be accessed and used by the web page.
Internet Explorer 8 Plus, Firefox, Opera, Chrome, and Safari support web storage.
Note: Internet Explorer 7 and earlier versions of IE do not support web storage.
The two objects in which the client stores data are:
Before using web storage, check that your browser supports localStorage and sessionStorage:
There is no time limit for the data stored by the localStorage object. The next day, the second week, or the following year, the data is still available.
The above examples can also be written as such:
Remove "lastname" from localStorage:
// 存储 localStorage.sitename = "W3Cschool在线教程"; // 查找 document.getElementById("result").innerHTML = localStorage.sitename;
Whether it's localStorage or sessionStorage, you can use the same APIs, with the following commonly used (in the case of localStorage):
Tip: Key/value pairs are usually stored as strings, and you can convert the format to your own needs.
The following example shows the number of times a user clicked a button.
String values in the code are converted to numeric types:
The sessionStorage method stores data for one session. When the user closes the browser window, the data is deleted.
How to create and access a sessionStorage: