DOM storage
Encyclopedia
Web Storage and DOM Storage (Document Object Model
Document Object Model
The Document Object Model is a cross-platform and language-independent convention for representing and interacting with objects in HTML, XHTML and XML documents. Aspects of the DOM may be addressed and manipulated within the syntax of the programming language in use...

) are web application
Web application
A web application is an application that is accessed over a network such as the Internet or an intranet. The term may also mean a computer software application that is coded in a browser-supported language and reliant on a common web browser to render the application executable.Web applications are...

 software methods and protocols used for storing data in a web browser
Web browser
A web browser is a software application for retrieving, presenting, and traversing information resources on the World Wide Web. An information resource is identified by a Uniform Resource Identifier and may be a web page, image, video, or other piece of content...

. Web storage supports persistent
Persistence (computer science)
Persistence in computer science refers to the characteristic of state that outlives the process that created it. Without this capability, state would only exist in RAM, and would be lost when this RAM loses power, such as a computer shutdown....

 data storage, similar to cookies
HTTP cookie
A cookie, also known as an HTTP cookie, web cookie, or browser cookie, is used for an origin website to send state information to a user's browser and for the browser to return the state information to the origin site...

 but with a greatly enhanced capacity and no information stored in the HTTP Request Header . There are 2 main web storage types, local storage and session storage, behaving like Persisted Cookies and Session Cookies accordingly.

Web storage is being standardized by the World Wide Web Consortium
World Wide Web Consortium
The World Wide Web Consortium is the main international standards organization for the World Wide Web .Founded and headed by Tim Berners-Lee, the consortium is made up of member organizations which maintain full-time staff for the purpose of working together in the development of standards for the...

 (W3C). It was originally part of the HTML 5
HTML 5
HTML5 is a language for structuring and presenting content for the World Wide Web, and is a core technology of the Internet originally proposed by Opera Software. It is the fifth revision of the HTML standard and is still under development...

 specification, but is now in a separate specification. It is supported by Internet Explorer 8
Internet Explorer 8
Windows Internet Explorer 8 is a web browser developed by Microsoft in the Internet Explorer browser series. The browser was released on March 19, 2009 for Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, and Windows 7. Both 32-bit and 64-bit builds are available...

, Mozilla
Mozilla
Mozilla is a term used in a number of ways in relation to the Mozilla.org project and the Mozilla Foundation, their defunct commercial predecessor Netscape Communications Corporation, and their related application software....

-based browsers (e.g., Firefox 2+
Mozilla Firefox 2
Mozilla Firefox 2 was a version of Firefox, a web browser released on October 24, 2006 by the Mozilla Corporation.Firefox 2 uses version 1.8 of the Gecko layout engine for displaying web pages...

, officially from 3.5
Mozilla Firefox 3.5
Mozilla Firefox 3.5 is a version of the Firefox web browser released in June 2009, adding a variety of new features to Firefox. Version 3.5 was touted as being twice as fast as 3.0...

), Safari
Safari (web browser)
Safari is a web browser developed by Apple Inc. and included with the Mac OS X and iOS operating systems. First released as a public beta on January 7, 2003 on the company's Mac OS X operating system, it became Apple's default browser beginning with Mac OS X v10.3 "Panther". Safari is also the...

 4, Google Chrome
Google Chrome
Google Chrome is a web browser developed by Google that uses the WebKit layout engine. It was first released as a beta version for Microsoft Windows on September 2, 2008, and the public stable release was on December 11, 2008. The name is derived from the graphical user interface frame, or...

 4 (sessionStorage is from 5), and Opera 10.50
Opera 10
Opera 10 is a version of the Opera web browser. The initial version, 10.00, was released in September 2009.This release added a variety of new features, a new skin designed by Jon Hicks, increased standards support, and a new application icon to Opera...

. only Opera supports the storage events.

Features

Web storage can be viewed simplistically as an improvement on cookies
HTTP cookie
A cookie, also known as an HTTP cookie, web cookie, or browser cookie, is used for an origin website to send state information to a user's browser and for the browser to return the state information to the origin site...

. However, it differs from cookies in some key ways.

Storage Size

Web Storage provides far greater storage capacity (5MB per domain
Domain name
A domain name is an identification string that defines a realm of administrative autonomy, authority, or control in the Internet. Domain names are formed by the rules and procedures of the Domain Name System ....

 in Mozilla Firefox
Mozilla Firefox
Mozilla Firefox is a free and open source web browser descended from the Mozilla Application Suite and managed by Mozilla Corporation. , Firefox is the second most widely used browser, with approximately 25% of worldwide usage share of web browsers...

, Google Chrome, and Opera
Opera (web browser)
Opera is a web browser and Internet suite developed by Opera Software with over 200 million users worldwide. The browser handles common Internet-related tasks such as displaying web sites, sending and receiving e-mail messages, managing contacts, chatting on IRC, downloading files via BitTorrent,...

, 10MB per storage area in Internet Explorer
Internet Explorer
Windows Internet Explorer is a series of graphical web browsers developed by Microsoft and included as part of the Microsoft Windows line of operating systems, starting in 1995. It was first released as part of the add-on package Plus! for Windows 95 that year...

) compared to 4KB (around 1000 times less space) available to cookies.

Client-side interface

Unlike cookies, which can be accessed by both the server and client side, Web storage falls exclusively under the purview of client-side scripting
Client-side scripting
Client-side scripting generally refers to the class of computer programs on the web that are executed client-side, by the user's web browser, instead of server-side...

. Web storage data is not transmitted to the server in every HTTP request, and a web server can't directly write to Web storage, but can of course issue read and write requests.

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 domain (it's available to all scripts from the domain that originally stored the data) and persists after the browser is closed. Session storage is per-page-per-window and is limited to the lifetime of the window. Session storage is intended to allow separate instances of the same web application to run in different windows without interfering with each other, a use case that's not well supported by cookies.

Interface and Data model

Web storage currently provides a better programmatic interface than cookies exposing an associative array
Associative array
In computer science, an associative array is an abstract data type composed of a collection of pairs, such that each possible key appears at most once in the collection....

 data model
Data model
A data model in software engineering is an abstract model, that documents and organizes the business data for communication between team members and is used as a plan for developing applications, specifically how data is stored and accessed....

 where the keys and values are both strings
String (computer science)
In formal languages, which are used in mathematical logic and theoretical computer science, a string is a finite sequence of symbols that are chosen from a set or alphabet....

. An additional API
Application programming interface
An application programming interface is a source code based specification intended to be used as an interface by software components to communicate with each other...

 for accessing structured data
Data model
A data model in software engineering is an abstract model, that documents and organizes the business data for communication between team members and is used as a plan for developing applications, specifically how data is stored and accessed....

, perhaps based on SQL
SQL
SQL is a programming language designed for managing data in relational database management systems ....

, is being considered by the W3C Web Applications Working Group.

Usage

Browsers that support web storage have the global variables 'sessionStorage' and 'localStorage' declared at the window level. The following JavaScript
JavaScript
JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles....

 code can be used on these browsers to trigger web storage behaviour:

sessionStorage


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

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

localStorage


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

// Retrieve value (works even after closing and re-opening the browser)
alert(localStorage.getItem('key'));

Accessing data for a particular domain

The following code can be used to retrieve all values stored in local storage for a particular domain (the domain for the web page that is being browsed).

This JavaScript code can be executed using development tools available in most modern browsers such as the IE Developer Toolbar
IE Developer Toolbar
Internet Explorer Developer Tools , is a component of Internet Explorer that aids in design and debugging of web pages. It was introduced as a toolbar for Internet Explorer 6 and Internet Explorer 7. Internet Explorer 8 and Internet Explorer 9 includes the features built in...

, Chrome
Chrome
-Materials:* Chromium, a chemical element* Chrome plating, a process of surfacing with chromium-Computing:* Google Chrome, a web browser** Chromium , the open-source counterpart to the Google Chrome web browser...

 Developer Tools, or Firebug
Firebug
The firebug, Pyrrhocoris apterus, is a common insect of the family Pyrrhocoridae. Easily recognizable due to its striking red and black colouration, it is distributed throughout the Palaearctic from the Atlantic coast of Europe to northwest China. It has also been reported from the USA, Central...

 extension in Firefox.:


var output = "LOCALSTORAGE DATA:\n------------------------------------\n";
if (localStorage) {
if (localStorage.length) {
for (var i = 0; i < localStorage.length; i++) {
output += localStorage[i] + ': ' + localStorage.getItem(localStorage[i]) + '\n';
}
} else {
output += 'There is no data stored for this domain.';
}
} else {
output += 'Your browser does not support local storage.'
}
alert(output);

Data Types

It is important to note that only strings can be stored via the Storage API . Attempting to store a different data type will result in an automatic conversion into a string in most browsers. Conversion into JavaScript Object Notation (JSON), however, allows for effective storage of JavaScript objects.


// Store an object instead of a string
localStorage.setItem('key', {name: 'value'});
alert(typeof localStorage['key']); // string

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

// Store an object using JSON
localStorage.setItem('key', '{"name":"value"}');
alert(eval(localStorage['key']).name); // value

Nomenclature

The W3C draft is titled "Web Storage", but "DOM storage" is also a commonly used name.

The "DOM" in DOM storage doesn't literally refer to the Document Object Model
Document Object Model
The Document Object Model is a cross-platform and language-independent convention for representing and interacting with objects in HTML, XHTML and XML documents. Aspects of the DOM may be addressed and manipulated within the syntax of the programming language in use...

. "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..."

Web Storage Management

Storage of Web Storage Objects is enabled per default in Mozilla Firefox
Mozilla Firefox
Mozilla Firefox is a free and open source web browser descended from the Mozilla Application Suite and managed by Mozilla Corporation. , Firefox is the second most widely used browser, with approximately 25% of worldwide usage share of web browsers...

 and Seamonkey
Seamonkey
Seamonkey may refer to:* Sea-Monkeys, a certain hybrid of brine shrimps* SeaMonkey, a web browser suite** Mozilla Application Suite or Seamonkey, a web browser suite and predecessor to SeaMonkey* The Amazing Live Sea Monkeys, a television series...

, but can be disabled by setting the "about:config" parameter "dom.storage.enabled" to false.

Mozilla Firefox stores all Web Storage objects in a single file named webappsstore.sqlite. The sqlite3 command can be used to show the elements stored therein.

There are browser extensions/add-ons for Google Chrome and Mozilla Firefox
Mozilla Firefox
Mozilla Firefox is a free and open source web browser descended from the Mozilla Application Suite and managed by Mozilla Corporation. , Firefox is the second most widely used browser, with approximately 25% of worldwide usage share of web browsers...

 available that let the user deal with web Storage, such as "Click&Clean","Click&Clean" extension for Google Chrome. Hotcleaner.com (2011-06-01). Retrieved on 2011-06-12. "BetterPrivacy" which can be configured to remove the whole Web Storage automatically on a regular basis.Mozilla add-ons page for "Better Privacy". Addons.mozilla.org. Retrieved on 2011-06-12.

Similar technologies

  • HTTP cookie
    HTTP cookie
    A cookie, also known as an HTTP cookie, web cookie, or browser cookie, is used for an origin website to send state information to a user's browser and for the browser to return the state information to the origin site...

    s
  • Indexed Database API
    Indexed Database API
    The Indexed Database API, or Indexed DB , is a proposed web browser standard interface for a local database of records holding simple values and hierarchical objects. Indexed DB was initially proposed by Oracle in 2009....

     (formerly WebSimpleDB)
  • Web SQL Database
    Web SQL Database
    Web SQL Database is a web page API for storing data in databases that can be queried using a variant of SQL.The API is supported by Google Chrome, Opera and Safari, but will not be implemented by Mozilla Firefox which instead supports the Indexed Database API.The W3C Web Applications Working...

  • Local Shared Object
    Local Shared Object
    Local Shared Objects , commonly called flash cookies are pieces of data that websites which use Adobe Flash may store on a user's computer...

    s in Adobe Flash
    Adobe Flash
    Adobe Flash is a multimedia platform used to add animation, video, and interactivity to web pages. Flash is frequently used for advertisements, games and flash animations for broadcast...

  • userData Behavior in Internet Explorer
  • Google Gears for IE, Firefox, Safari and Windows Mobile

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK