Web storage explained

Web storage, sometimes known as DOM storage (Document Object Model storage), is a standard JavaScript API provided by web browsers. It enables websites to store persistent data on users' devices similar to cookies, but with much larger capacity and no information sent in HTTP headers.[1] There are two main web storage types: local storage and session storage, behaving similarly to persistent cookies and session cookies respectively. Web Storage is standardized by the World Wide Web Consortium (W3C)[2] and WHATWG,[3] and is supported by all major browsers.

Features

Web storage differs from cookies in some key ways.

Purpose

Cookies are intended for communication with servers; they are automatically added to all requests and can be accessed by both the server and client-side. Web storage falls exclusively under the purview of client-side scripting. Web storage data is not automatically transmitted to the server in every HTTP request, and a web server can't directly write to Web storage. However, either of these effects can be achieved with explicit client-side scripts, allowing for fine-tuning the server's desired interaction.

Storage size

Cookies are restricted to 4 kilobytes. Web storage provides far greater storage capacity:

Local and session storage

Web storage offers two different storage areas—local storage and session storage—which differ in scope and lifetime. Data placed in local storage is per origin—the combination of protocol, host name, and port number as defined in the same-origin policy. The data is available to all scripts loaded from pages from the same origin that previously stored the data and persists after the browser is closed. As such, Web storage does not suffer from cookie Weak Integrity and Weak Confidentiality issues, described in sections 8.5 and 8.6. Session storage is both per-origin and per-instance (per-window or per-tab) and is limited to the lifetime of the instance. Session storage is intended to allow separate instances of the same web app to run in different windows without interfering with each other, a use case that's not well supported by cookies.[9]

Interface and data model

Web storage provides a better programmatic interface than cookies because it exposes an associative array data model where the keys and values are both strings.

Usage

Browsers that support web storage have the global objects sessionStorage and localStorage declared at the window level. The following JavaScript code can be used on these browsers to trigger web storage behavior:// Store value on browser for duration of the sessionsessionStorage.setItem('key', 'value');

// Retrieve value (gets deleted when browser is closed and re-opened) ...alert(sessionStorage.getItem('key'));

// Store value on the browser beyond the duration of the sessionlocalStorage.setItem('key', 'value');

// Retrieve value (persists even after closing and re-opening the browser)alert(localStorage.getItem('key'));Only strings can be stored via the Storage API.[10] Attempting to store a different data type will result in an automatic conversion into a string in most browsers. Conversion into JSON, however, allows for effective storage of JavaScript objects.

// Store an object instead of a stringlocalStorage.setItem('key',);alert(typeof localStorage.getItem('key')); // string

// Store an integer instead of a stringlocalStorage.setItem('key', 1);alert(typeof localStorage.getItem('key')); // string

// Store an object using JSONlocalStorage.setItem('key', JSON.stringify);alert(JSON.parse(localStorage.getItem('key')).name); // value

Nomenclature

The W3C draft is titled "Web Storage". "DOM storage" has also been a commonly used name, though it is becoming less so; for example the "DOM Storage" web articles of the Mozilla and Microsoft developer sites have been replaced with "Web Storage" articles.[11] [12] [13] [14]

The "DOM" in DOM storage does not literally refer to the Document Object Model. According to the W3C, "The term DOM is used to refer to the API set made available to scripts in Web applications, and does not necessarily imply the existence of an actual Document object..."[15]

Web storage management

Storage of web storage objects is enabled by default in current versions of all supporting web browsers, with browser vendors providing ways for users natively to enable or disable web storage, or clear the web storage "cache".[16] Similar controls over web storage are also available through 3rd party browser extensions. Each browser stores Web storage objects differently:

Similar Technologies

While localStorage is often used for storing persistent key-value pairs, other APIs have emerged to enable various use cases like iteratable indexing[19] and with different performance patterns:

External links

Notes and References

  1. Web site: localStorage is not cookies. https://web.archive.org/web/20110602165051/http://blog.andyhume.net/localstorage-is-not-cookies. dead. Andy. Hume. 2011-06-02. 2011-03-24. 2021-05-14. andyhume.net.
  2. Web site: Web Storage (Second Edition). Ian. Hickson. Web Platform Working Group. W3C. 2021-01-28. 2021-05-14.
  3. Web site: HTML Standard § 12 Web storage. html.spec.whatwg.org. WHATWG. 2021-05-14.
  4. Web site: Web Storage: Easier, More Powerful Client-Side Data Storage. Shwetank. Dixit. 2013-03-05. 2021-05-14. Dev.Opera.
  5. Web site: Working with quota on mobile browsers: A research report on browser storage - HTML5 Rocks. https://web.archive.org/web/20140201165757/https://www.html5rocks.com/en/tutorials/offline/quota-research/. dead. Eiji. Kitamura. 2014-01-28. 2021-05-04. 2014-02-01.
  6. http://ejohn.org/blog/dom-storage/ John Resig: DOM Storage
  7. Web site: Issue 21680002: Up the window.localstorage limit to 10M from 5M. - Code Review. Chromium Code Reviews. 2013-03-08. 2021-05-14. michaeln.
  8. Web site: Introduction to Web Storage. Microsoft. Microsoft. Microsoft Docs. 2016-10-20. 2021-05-14.
  9. http://dev.w3.org/html5/webstorage/#introduction W3C: Web Storage draft standard
  10. W3C, 2011 http://dev.w3.org/html5/webstorage/
  11. Web site: DOM Storage . Mozilla Developer Network . 2011-06-12. https://web.archive.org/web/20110604140851/https://developer.mozilla.org/En/DOM:Storage . June 4, 2011 . dead.
  12. Web site: Web Storage API . Mozilla Developer Network . June 28, 2017.
  13. Web site: Introduction to DOM Storage . Microsoft Developer Network . 2011-06-12 . https://web.archive.org/web/20110608180129/http://msdn.microsoft.com/en-us/library/cc197062(VS.85).aspx . June 8, 2011 . dead.
  14. Web site: Introduction to Web Storage . Microsoft Developer Network . June 28, 2017.
  15. http://dev.w3.org/html5/webstorage/#terminology W3C: Web Storage draft standard
  16. https://mid.as/kb/00103/enable-disable-or-clear-web-storage-cache How to enable, disable, or clear your browser's "Web Storage" cache
  17. https://kb.mozillazine.org/Webappsstore.sqlite Webappsstore.sqlite
  18. https://discussions.apple.com/thread/7772494 Where is Safari web data stored?
  19. Web site: localStorage in Modern Applications . RxDB . August 14, 2023.