The difference between cookie, localStorage, and sessionStorage
1.localStorage
Features:
-
Storage control is 5M
-
The number of stored items is affected by the storage space
-
Can add, delete, modify and check
-
The stored data only supports strings ( emphasis!!!!!! )
- JSON.parse(json format string)
- JSON.stringify()
-
follow the same-origin policy
-
As long as it is in the same source URL, the data in localStorage can be shared
-
Will be consistent in the browser, unless actively destroyed removeItem() / clear()
Commonly used methods:
window.localStorage.setItem(key,value);//设置存储的内容
window.localStorage.getItem(key);//获取内容
window.localStorage.removeItem(key);//删除内容
window.localStorage.clear();//清空内容
Same Origin Policy
Whether the two URLs are of the same origin depends on whether the http protocol, domain name/ip address, and port number are all consistent
The composition of url
http protocol, domain name/IP address, port number, path, query parameters/dynamic parameters
2.sessionStorage
Features:
-
The life cycle is to close the current browser window
-
can be accessed under the same window
-
The data size is about 5M
-
Temporary storage: When the browser is closed or the tab is closed, the local storage content will be destroyed
-
Stored data type: just like localStorage, it can only be a string
-
sessionStorage: does not comply with the same-origin policy
-
Data sharing is not supported, and same-origin pages do not support data sharing
Commonly used methods:
window.sessionStorage.setItem(key,value);//设置存储的内容
window.sessionStorage.getItem(key);//获取内容
window.sessionStorage.removeItem(key);//删除内容
3.cookie
features
- Stored on the client, does not occupy server resources
- The storage capacity is limited, generally 4KB
- Can only be in string format
Composition of cookies
-
key: key
-
value: value
-
domain: Domain, expressing which website this cookie belongs to
-
path: path, expressing which base path this cookie belongs to the website, just like different departments of the same company will issue different pass. For example
/news
, it means that this cookie belongs to/news
this path. -
secure: whether to use secure transport
-
expire: expiration time, indicating when the cookie expires
4. The difference between cookie, localStorage and sessionStorage
characteristic | cookie | localStorage | sessionStorage |
---|---|---|---|
life cycle | You can set the expiration time. If there is no setting, the default is to expire after closing the browser. | Stored permanently unless cleared | Valid only in the current session, it will be cleared after closing the page or browser |
Store data size | around 4k | Usually 5MB | Usually 5MB |
HTTP request | It will be carried in the HTTP header every time. If you use cookies to save too much data, it will cause performance problems | It is only saved in the client (ie browser), and does not participate in communication with the server | It is only saved in the client (ie browser), and does not participate in communication with the server |
ease of use | It needs to be encapsulated by itself, and the native cookie interface is not friendly | The native interface can be accepted or re-encapsulated | The native interface can be accepted or re-encapsulated |