js data storage

1.Cookie
HTTP Cookie, usually directly called the Cookie, originally used to store session information in the client.
The need to develop the scope, not cross-domain access;
can not save too much data (maximum of only 4KB);
locally saved data will be sent to the server, a waste of bandwidth;

2.sessionStorage
for a locally stored data session (session) is, after closing the window, i.e. the sessionStorage destruction, in addition to the protocol, the host name, the port, the same window is also required (i.e. the browser tab), the You can read / modify a sessionStorage to the same data.

3.localStorage
persistent client-side storage, unless the initiative to delete data, or the data is never out of date.
Information stored in the same domain is shared. localStorage as long as the same agreement, the same host name, the same port can read / modify a localStorage to the same data.

Setting: lacalStorage.setItem('key','value');Or localStorage.name=value;
get: localStorage.getItem('key');or localStorage.name;
Delete: localStorage.remove('key');or delete localStorage.name;
remove all:localStorage.clear();

sessionStorage and work just like localStorage.

The same three points:
(browser) to store data locally.

Three different points:
Cookie: Size 4kb, time has expired, each request to the server, increasing the request, the page poor performance
localStorage: size 5M, no expiration date, not sent to the server, is not compatible with IE6
sessionStorage: size 5M there expiration time, not sent to the server

发布了47 篇原创文章 · 获赞 4 · 访问量 6684

Guess you like

Origin blog.csdn.net/lifangfang0607/article/details/102761863