Local Storage, Session storage, Web storage, web database and cookies in HTML5
What is the difference between these concepts, and when should I use one in particular? Does this listing also contain differen开发者_JAVA百科t names for the same general concept?
- HTML5 local storage
- HTML5 session storage
- HTML5 web storage
- HTML5 web database
- Cookies
HTML5 web storage is a generic umbrella term for the new client-side data storage options.
Local Storage is persistent and scoped to the domain. At the moment two flavors are usually mentioned:
- 'default': stores things in name/value pairs
- Web SQL (aka Web Database): uses an SQL database
Session Storage is non persistent and scoped only to the current window.
Cookies are the old school way of doing all of the above. Stores name/value pairs per domain.
I would like to add more information:
cookies are able to store only 4k of data
whereas localStorage
is able to store 5mb of data (Depending on browsers)
Websites will save cookies in browsers and next time browser will send that cookie
along with http request to be used server-side. Cookies are meant for being used with the server.
With localStorage
, you can store more data, but it is restricted to the client by default.
Session Storage:Session storage is introduced where the user is carrying out a single transaction, but could be carrying out multiple transactions in different windows at the same time.Session is terminated once we close the window.
Local Storage:Local storage is specific to domain and is introduced to span across multiple windows.There is no time limit as in the case of Cookies,and can store upto 5MB storage such as Users MailBox etc....
AFAIC:
- Cookies are 4k per cookie, and local storage is 5k per domain.
- Cookies existance time limits and sorage is just client-side protocol- and domain-specific bin for data.
Another big thing to consider if your users are located in Europe, is that Cookies are illegal in Europe. https://www.sitepoint.com/europe-website-cookie-privacy-law/
精彩评论