Introduction and difference between webstorage, cookie, sessionStorage and localStorage

webstorage local storage

Webstorage is local storage, stored on the client, including localStorage and sessionStorage.
1. The storage size is basically 5MB, only stored on the client
2. localStorage and sessionStorage can only store string types. For complex objects, you can use stringify and parse of the JSON object provided by ECMAScript to process
3. Obtaining methods:
localStorage: window .localStorage;
sessionStorage: window.sessionStorage;
4. Both return an object of storage type

The difference between cookie, sessionStorage and localStorage

Common point: They are all stored on the browser side and are of the same origin.

The cookie data is always carried in the same-origin http request (even if it is not needed), that is, it is transmitted back and forth between the server and the client. There is a concept of path, and the storage capacity is rarely about 4k. The sessionStorage and localStorage are only stored locally, and will not automatically transfer data to the server.

sessionStorage is only valid before the current browser window is closed, and cannot be saved persistently. The localStorage window or the browser closes are always valid and is used for persistent storage. The cookie is valid until the set cookie expiration time, even if the window or browser is closed.

Both localStorage and cookie are shared in the same-origin window. sessionStorage is not shared.

Cookies are not safe. Compared with localStorage and sessionStorage, they are safer, and the speed of webstorage is faster than cookies because it is directly obtained from the local.

Guess you like

Origin blog.csdn.net/d1063270962/article/details/109602256