This is the 16th day of my participation in the August Text Challenge.More challenges in August

In older browsers, JS provides sessionStorage and globalStorage. LocalStorage is provided in HTML5 to replace globalStorage.

Html5 Web Storage includes two Storage methods: sessionStorage and localStorage.

  • SessionStorage is used to store data in a session locally, which can only be accessed by pages in the same session and is destroyed when the session ends. Therefore, sessionStorage is not a persistent local storage, only session level storage.

  • LocalStorage localStorage for persistence, data will never expire unless actively deleted.

Difference between Web storage and cookies

The concept of Web Storage is similar to cookie, except that it is designed for larger Storage capacity.

Cookie size is limited, and every time you request a new page when the Cookie will be sent in the past, so virtually waste bandwidth, in addition to Cookie also need to specify scope, can not be called across the domain.

In addition, Web Storage with setItem, the getItem, removeItem, methods of the clear, don’t like cookies need encapsulation setCookie front-end developer himself, getCookie.

But cookie is also not possible or missing: the role of cookie is to interact with the server, as part of the HTTP specification exists, and Web Storage is only in order to “store” data in the local.

Browser support in addition to IE7 and the following does not support, other standard browsers are fully supported (IE and FF need to run in the Web server), it is worth mentioning that IE is always good, such as IE7, IE6 userData is actually javascript local storage solution. Through simple code encapsulation can be unified to all browsers support Web storage.

Both localStorage and sessionStorage have the same operation methods, such as setItem, getItem, and removeItem

The difference between cookies and sessions

  • Cookie data is stored on the client’s browser and session data is stored on the server.
  • Cookies are not very secure, so someone can analyze cookies that are stored locally and do cookie spoofing and you should use sessions for security purposes.
  • Sessions are stored on the server for a certain amount of time. Cookies should be used to reduce server performance when the number of accesses increases.
  • A single cookie can hold no more than 4K of data, and many browsers limit the number of cookies a site can hold to 20.

Therefore, personal advice: store important information such as login information as session and other information in cookies if necessary