localStorage,sessionStorage,cookie

I was once asked about the difference between these three. Although I said it, it was not systematic enough. Reorganize here. . .

local storage

Includes: localStorage and sessionStorage

The size limit of both of them is 5M, which is only saved in the client (ie the browser) and does not participate in the communication with the server. If it exceeds, an Uncaught QuotaExceededErrorerror will be reported. Therefore, during development, attention should be paid to controlling the storage data to be kept within the limit size, and periodically clearing useless data.

Note: This website specializes in collecting statistics and statistics on the local storage space limitations of the browsers used by visitors. Currently, it seems that only the data of Android and Chrome browsers are stored. For details, please see: http://dev-test.nemikor.com/ web-storage/support-test/

The difference is: sessionStorage is cleared when the page or browser is closed. localStorage does not, the lifetime is permanent unless cleared by the user. At the same time, local storage cannot be shared between different browsers. However, localStorage can be shared between different pages, and sessionStorage information cannot be shared between different pages or tabs

How to use: (localStorage and sessionStorage have the same API, just replace localStorage with sessionStorage)

localStorage.setItem("key","value");//Store a value "value" with "key" as the name
localStorage.getItem("key");//Get the value named "key"
localStorage.removeItem("key");//Remove the information named "key".
localStorage.clear();​//Clear all information in localStorage

Cookie

HTTP is a stateless protocol. If the server only connects from the network, it cannot identify the user's state. Cookie is to identify the data of different users.

Cookie refers to the data stored on the user's local terminal by some websites in order to identify the user's identity and perform session tracking.

Lifetime: Only valid until the set cookie expiration time, even if the window or browser is closed.

The storage data size is about 4K. The length of each cookie cannot exceed 4KB, otherwise it will be truncated. So how to intercept it?

In Internet Explorer and Opera, old cookies will be intercepted to make room for new cookies;

In Firefox, although the last set cookie is always kept, it is randomly determined which cookies are kept.

Personally, I think the cookie API is not very easy to use, so in general, it will be repackaged and used.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325687525&siteId=291194637