Home >Web Front-end >JS Tutorial >The difference between localStorage. sessionStorage and Cookie
●Differences in storage size:
The size of localStorage is generally 5M
The size of sessionStorage is generally 5M
The size of cookies is generally 4K
●Different validity periods:
1. The validity period of localStorage is permanent unless you delete it manually.
2.sessionStorage is valid in the current session and will be cleared when the page or browser is closed.
3. Cookies are valid before the set validity period, and will expire when the validity period expires.
●Communication with the server
1.localStorage does not participate in server-side communication.
2.sessionStorage does not participate in server-side communication.
3. Cookies participate in server-side communication and will be stored in the http header information every time. (If you use cookies to save too much data, it will cause performance problems)
●Detailed explanation of the difference between the scopes of localStorage and sessionStorage 1. Different browsers cannot share information in localStorage or sessionStorage. 2. Different pages of the same browser can share the same localStorage (the pages belong to the same domain name and port), but sessionStorage information cannot be shared between different pages or tabs.
Generally store things like shopping carts, because the validity period is permanent and the size is about 5m, so localStorage is more commonly used in the front end, cookie It is not used much because it is not safe. It is generally used as a secret key (token), the back-end stores the password, and gives the front-end token to open
Recommended tutorial: "JS Tutorial》
The above is the detailed content of The difference between localStorage. sessionStorage and Cookie. For more information, please follow other related articles on the PHP Chinese website!