Jump to content

Web storage: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m Reverted edits by 216.224.253.216 (talk) (AV)
m Grammarly
Tag: Reverted
Line 3: Line 3:
{{HTML}}
{{HTML}}


'''Web storage''', sometimes known as '''DOM storage''' ([[Document Object Model]] storage), is a standard [[JavaScript]] API provided by [[web browser]]s. It enables [[website]]s to store [[Persistence (computer science)|persistent]] data on users' devices similar to [[HTTP cookie|cookies]], but with much larger capacity<ref name="dev-opera-ws" /> and no information sent in [[List of HTTP headers|HTTP headers]].<ref>{{cite web|url=http://blog.andyhume.net/localstorage-is-not-cookies|archive-url=https://web.archive.org/web/20110602165051/http://blog.andyhume.net/localstorage-is-not-cookies|url-status=dead|title=localStorage is not cookies|author-first=Andy|author-last=Hume|archive-date=2011-06-02|date=2011-03-24|access-date=2021-05-14|website=andyhume.net}}</ref> There are two main web storage types: local storage and session storage, behaving similarly to [[persistent cookie]]s and [[session cookie]]s respectively. Web Storage is standardized by the [[World Wide Web Consortium]] (W3C)<ref>{{Cite web|url=http://www.w3.org/TR/webstorage/|title=Web Storage (Second Edition)|editor-first=Ian|editor-last=Hickson|publisher=Web Platform Working Group|website=W3C|date=2021-01-28|access-date=2021-05-14}}</ref> and [[WHATWG]],<ref>{{Cite web|url=https://html.spec.whatwg.org/multipage/webstorage.html|title=HTML Standard § 12 Web storage|website=html.spec.whatwg.org|author=WHATWG|access-date=2021-05-14}}</ref> and is supported by all major browsers.
'''Web storage''', sometimes known as '''DOM storage''' ([[Document Object Model]] storage), is a standard [[JavaScript]] API provided by [[web browser]]s. It enables [[website]]s to store [[Persistence (computer science)|persistent]] data on users' devices similar to [[HTTP cookie|cookies]], but with much larger capacity<ref name="dev-opera-ws" /> and no information sent in [[List of HTTP headers|HTTP headers]].<ref>{{site web|url=http://blog.andyhume.net/localstorage-is-not-cookies|archive-url=https://web.archive.org/web/20110602165051/http://blog.andyhume.net/localstorage-is-not-cookies|url-status=dead|title=localStorage is not cookies|author-first=Andy|author-last=Hume|archive-date=2011-06-02|date=2011-03-24|access-date=2021-05-14|website=andyhume.net}}</ref> There are two main web storage types: local storage and session storage, behaving similarly to [[persistent cookie]]s and [[session cookie]]s respectively. Web Storage is standardized by the [[World Wide Web Consortium]] (W3C)<ref>{{Cite web|url=http://www.w3.org/TR/webstorage/|title=Web Storage (Second Edition)|editor-first=Ian|editor-last=Hickson|publisher=Web Platform Working Group|website=W3C|date=2021-01-28|access-date=2021-05-14}}</ref> and [[WHATWG]],<ref>{{Cite web|url=https://html.spec.whatwg.org/multipage/webstorage.html|title=HTML Standard § 12 Web storage|website=html.spec.whatwg.org|author=WHATWG|access-date=2021-05-14}}</ref> and is supported by all major browsers.


==Features==
==Features==
Line 9: Line 9:


===Purpose===
===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.
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===
===Storage size===
Line 16: Line 16:
* [[Safari (web browser)|Safari]] 8 allows 5 MB<ref name="html5rocks-quota-research">{{Cite web|url=https://www.html5rocks.com/en/tutorials/offline/quota-research/|archive-url=https://web.archive.org/web/20140201165757/https://www.html5rocks.com/en/tutorials/offline/quota-research/|url-status=dead|title=Working with quota on mobile browsers: A research report on browser storage - HTML5 Rocks|author-first=Eiji|author-last=Kitamura|publication-date=2014-01-28|access-date=2021-05-04|archive-date=2014-02-01}}</ref>
* [[Safari (web browser)|Safari]] 8 allows 5 MB<ref name="html5rocks-quota-research">{{Cite web|url=https://www.html5rocks.com/en/tutorials/offline/quota-research/|archive-url=https://web.archive.org/web/20140201165757/https://www.html5rocks.com/en/tutorials/offline/quota-research/|url-status=dead|title=Working with quota on mobile browsers: A research report on browser storage - HTML5 Rocks|author-first=Eiji|author-last=Kitamura|publication-date=2014-01-28|access-date=2021-05-04|archive-date=2014-02-01}}</ref>
* [[Firefox]] 34 allows 10 MB<ref name="html5rocks-quota-research"/> (formerly 5 MB per [[same-origin policy|origin]] in 2007<ref>[http://ejohn.org/blog/dom-storage/ John Resig: DOM Storage]. John Resig, ''ejohn.org''. Retrieved on 2011-06-12.</ref>)
* [[Firefox]] 34 allows 10 MB<ref name="html5rocks-quota-research"/> (formerly 5 MB per [[same-origin policy|origin]] in 2007<ref>[http://ejohn.org/blog/dom-storage/ John Resig: DOM Storage]. John Resig, ''ejohn.org''. Retrieved on 2011-06-12.</ref>)
* [[Google Chrome]] allows 10 MB per origin (formerly 5 MB per origin)<ref>{{cite web|url=https://chromiumcodereview.appspot.com/21680002|title=Issue 21680002: Up the window.localstorage limit to 10M from 5M. - Code Review|website=Chromium Code Reviews|date=2013-03-08|access-date=2021-05-14|author=michaeln}}</ref>
* [[Google Chrome]] allows 10 MB per origin (formerly 5 MB per origin)<ref>{{cite web|url=https://chromiumcodereview.appspot.com/21680002|title=Issue 21680002: Up the window.local storage limit to 10M from 5M. - Code Review|website=Chromium Code Reviews|date=2013-03-08|access-date=2021-05-14|author=michaeln}}</ref>
* [[Internet Explorer]] allows 10 MB per storage area<ref>{{cite web|url=https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/bg142799(v=vs.85)|title=Introduction to Web Storage|author=Microsoft|publisher=Microsoft|website=Microsoft Docs|date=2016-10-20|access-date=2021-05-14}}</ref>
* [[Internet Explorer]] allows 10 MB per storage area<ref>{{cite web|url=https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/bg142799(v=vs.85)|title=Introduction to Web Storage|author=Microsoft|publisher=Microsoft|website=Microsoft Docs|date=2016-10-20|access-date=2021-05-14}}</ref>


===Local and session storage===
===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 {{IETF RFC|6265}} 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.<ref>[http://dev.w3.org/html5/webstorage/#introduction W3C: Web Storage draft standard]. Dev.w3.org (2004-02-05). Retrieved on 2011-06-12.</ref>
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, hostname, 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 {{IETF RFC|6265}} 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.<ref>[http://dev.w3.org/html5/webstorage/#introduction W3C: Web Storage draft standard]. Dev.w3.org (2004-02-05). Retrieved on 2011-06-12.</ref>
===Interface and data model===
===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 [[String (computer science)|strings]].
Web storage provides a better programmatic interface than cookies because it exposes an [[associative array]] [[data model]] where the keys and values are both [[String (computer science)|strings]].


==Usage==
==Usage==
Browsers that support web storage have the global objects <code>sessionStorage</code> and <code>localStorage</code> declared at the window level. The following [[JavaScript]] code can be used on these browsers to trigger web storage behavior:<syntaxhighlight lang="javascript">
Browsers that support web storage have the global objects <code>sessionStorage</code> and <code>localStorage</code> declared at the window level. The following [[JavaScript]] code can be used on these browsers to trigger web storage behaviour:<syntaxhighlight lang="javascript">
// Store value on browser for duration of the session
// Store value on the browser for the duration of the session
sessionStorage.setItem('key', 'value');
sessionStorage.setItem('key', 'value');


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


Line 56: Line 56:
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.<ref>{{Cite web |url=https://developer.mozilla.org/En/DOM:Storage |title=DOM Storage |website=Mozilla Developer Network |access-date=2011-06-12|archive-url=https://web.archive.org/web/20110604140851/https://developer.mozilla.org/En/DOM:Storage |archive-date=June 4, 2011 |url-status=dead}}</ref><ref>{{Cite web |url=https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API |title=Web Storage API |website=Mozilla Developer Network |access-date=June 28, 2017}}</ref><ref>{{Cite web |url=http://msdn.microsoft.com/en-us/library/cc197062(VS.85).aspx |title=Introduction to DOM Storage |website=Microsoft Developer Network |access-date=2011-06-12 |archive-url=https://web.archive.org/web/20110608180129/http://msdn.microsoft.com/en-us/library/cc197062(VS.85).aspx |archive-date=June 8, 2011 |url-status=dead}}</ref><ref>{{Cite web |url=https://msdn.microsoft.com/en-us/library/bg142799(v=vs.85).aspx |title=Introduction to Web Storage |website=Microsoft Developer Network |access-date=June 28, 2017}}</ref>
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.<ref>{{Cite web |url=https://developer.mozilla.org/En/DOM:Storage |title=DOM Storage |website=Mozilla Developer Network |access-date=2011-06-12|archive-url=https://web.archive.org/web/20110604140851/https://developer.mozilla.org/En/DOM:Storage |archive-date=June 4, 2011 |url-status=dead}}</ref><ref>{{Cite web |url=https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API |title=Web Storage API |website=Mozilla Developer Network |access-date=June 28, 2017}}</ref><ref>{{Cite web |url=http://msdn.microsoft.com/en-us/library/cc197062(VS.85).aspx |title=Introduction to DOM Storage |website=Microsoft Developer Network |access-date=2011-06-12 |archive-url=https://web.archive.org/web/20110608180129/http://msdn.microsoft.com/en-us/library/cc197062(VS.85).aspx |archive-date=June 8, 2011 |url-status=dead}}</ref><ref>{{Cite web |url=https://msdn.microsoft.com/en-us/library/bg142799(v=vs.85).aspx |title=Introduction to Web Storage |website=Microsoft Developer Network |access-date=June 28, 2017}}</ref>


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..."<ref>[http://dev.w3.org/html5/webstorage/#terminology W3C: Web Storage draft standard]. Dev.w3.org (2004-02-05). Retrieved on 2011-06-12.</ref>
The "DOM" in DOM storage does not 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..."<ref>[http://dev.w3.org/html5/webstorage/#terminology W3C: Web Storage draft standard]. Dev.w3.org (2004-02-05). Retrieved on 2011-06-12.</ref>


==Web storage management==
==Web storage management==
Line 65: Line 65:
* [[Opera (web browser)|Opera]]'s Web storage is located in either "<code>\AppData\Roaming\Opera\Opera\sessions\autosave.win</code>" or "<code>\AppData\Local\Opera\Opera\pstorage\</code>" depending upon Opera's version.
* [[Opera (web browser)|Opera]]'s Web storage is located in either "<code>\AppData\Roaming\Opera\Opera\sessions\autosave.win</code>" or "<code>\AppData\Local\Opera\Opera\pstorage\</code>" depending upon Opera's version.
* [[Internet Explorer]]'s Web storage is "<code>\AppData\LocalLow\Microsoft\Internet Explorer\DOMStorage</code>".
* [[Internet Explorer]]'s Web storage is "<code>\AppData\LocalLow\Microsoft\Internet Explorer\DOMStorage</code>".
* [[Safari (web browser)|Safari]]'s Web Storage is located in a folder labeled "<code>LocalStorage</code>" within a hidden "<code>safari</code>" folder. <ref>[https://discussions.apple.com/thread/7772494 Where is Safari web data stored?] discussions.apple.com. Retrieved 20 2022-10-06</ref>
* [[Safari (web browser)|Safari]]'s Web Storage is located in a folder labelled "<code>LocalStorage</code>" within a hidden "<code>safari</code>" folder. <ref>[https://discussions.apple.com/thread/7772494 Where is Safari web data stored?] discussions.apple.com. Retrieved 20 2022-10-06</ref>


==Similar Technologies==
==Similar Technologies==
Line 85: Line 85:
*[https://developer.blackberry.com/html5/apis/v2_2/localstorage.html Local Storage] on BlackBerry DevZone
*[https://developer.blackberry.com/html5/apis/v2_2/localstorage.html Local Storage] on BlackBerry DevZone
<!--
<!--
* [http://www.domcached.com/ DOMCached - a memcached like caching system for JavaScript using DOM storage]
* [http://www.domcached.com/ DOMCached - a memcached-like caching system for JavaScript using DOM storage]
* Dive Into JavaScript: [http://www.diveintojavascript.com/javascript-apis/web-storage-dom-storage Web Storage (DOM Storage) Reference] and [http://www.diveintojavascript.com/tutorials/web-storage-tutorial-creating-an-address-book-application Web Storage Tutorial: Creating an Address Book Application]
* Dive Into JavaScript: [http://www.diveintojavascript.com/javascript-apis/web-storage-dom-storage Web Storage (DOM Storage) Reference] and [http://www.diveintojavascript.com/tutorials/web-storage-tutorial-creating-an-address-book-application Web Storage Tutorial: Creating an Address Book Application]
-->
-->

Revision as of 14:26, 23 January 2024

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[1] and no information sent in HTTP headers.[2] 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)[3] and WHATWG,[4] 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, hostname, 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 RFC 6265 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 behaviour:

// Store value on the browser for the duration of the session
sessionStorage.setItem('key', 'value');

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

// Store value on the browser beyond the duration of the session
localStorage.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 string
localStorage.setItem('key', {name: 'value'});
alert(typeof localStorage.getItem('key')); // string

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

// Store an object using JSON
localStorage.setItem('key', JSON.stringify({name: 'value'}));
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 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 to natively 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:

  • Firefox saves Web storage objects in a SQLite file called webappsstore.sqlite in the user's profile folder. [17]
  • Google Chrome records Web storage data in a SQLite file in the user's profile. The subfolder containing this file is "\AppData\Local\Google\Chrome\User Data\Default\Local Storage" on Windows, and "~/Library/Application Support/Google/Chrome/Default/Local Storage" on macOS.
  • Opera's Web storage is located in either "\AppData\Roaming\Opera\Opera\sessions\autosave.win" or "\AppData\Local\Opera\Opera\pstorage\" depending upon Opera's version.
  • Internet Explorer's Web storage is "\AppData\LocalLow\Microsoft\Internet Explorer\DOMStorage".
  • Safari's Web Storage is located in a folder labelled "LocalStorage" within a hidden "safari" folder. [18]

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:

References

  1. ^ a b Dixit, Shwetank (2013-03-05). "Web Storage: Easier, More Powerful Client-Side Data Storage". Dev.Opera. Retrieved 2021-05-14.
  2. ^ Hume, Andy (2011-03-24). "localStorage is not cookies". andyhume.net. Archived from the original on 2011-06-02. Retrieved 2021-05-14.
  3. ^ Hickson, Ian, ed. (2021-01-28). "Web Storage (Second Edition)". W3C. Web Platform Working Group. Retrieved 2021-05-14.
  4. ^ WHATWG. "HTML Standard § 12 Web storage". html.spec.whatwg.org. Retrieved 2021-05-14.
  5. ^ a b Kitamura, Eiji (2014-01-28). "Working with quota on mobile browsers: A research report on browser storage - HTML5 Rocks". Archived from the original on 2014-02-01. Retrieved 2021-05-04.
  6. ^ John Resig: DOM Storage. John Resig, ejohn.org. Retrieved on 2011-06-12.
  7. ^ michaeln (2013-03-08). "Issue 21680002: Up the window.local storage limit to 10M from 5M. - Code Review". Chromium Code Reviews. Retrieved 2021-05-14.
  8. ^ Microsoft (2016-10-20). "Introduction to Web Storage". Microsoft Docs. Microsoft. Retrieved 2021-05-14.
  9. ^ W3C: Web Storage draft standard. Dev.w3.org (2004-02-05). Retrieved on 2011-06-12.
  10. ^ W3C, 2011 http://dev.w3.org/html5/webstorage/
  11. ^ "DOM Storage". Mozilla Developer Network. Archived from the original on June 4, 2011. Retrieved 2011-06-12.
  12. ^ "Web Storage API". Mozilla Developer Network. Retrieved June 28, 2017.
  13. ^ "Introduction to DOM Storage". Microsoft Developer Network. Archived from the original on June 8, 2011. Retrieved 2011-06-12.
  14. ^ "Introduction to Web Storage". Microsoft Developer Network. Retrieved June 28, 2017.
  15. ^ W3C: Web Storage draft standard. Dev.w3.org (2004-02-05). Retrieved on 2011-06-12.
  16. ^ How to enable, disable, or clear your browser's "Web Storage" cache. mid.as. Retrieved on 2022-10-06.
  17. ^ Webappsstore.sqlite kb.mozillazine.org
  18. ^ Where is Safari web data stored? discussions.apple.com. Retrieved 20 2022-10-06
  19. ^ "localStorage in Modern Applications". RxDB. Retrieved August 14, 2023.

External links