DOM Storage is the name given to the set of storage-related features first introduced in the Web Applications 1.0 specification, and now split off into its own W3C Web Storage specification. DOM Storage is designed to provide a larger, securer, and easier-to-use alternative to storing information in cookies. It is currently only available in Mozilla- and WebKit-based browsers, notably starting with Firefox 2 and Safari 4.
The DOM Storage mechanism is a means through which string key/value pairs can be securely stored and later retrieved for use. The goal of this addition is to provide a comprehensive means through which interactive applications can be built (including advanced abilities, such as being able to work "offline" for extended periods of time).
Currently, only Mozilla-based browsers, Internet Explorer 8 beta 2, and Safari (not other webkit-based browsers, e.g., Google Chrome) provide a working implementation of the DOM Storage specification. Internet Explorer previous to version 8 does have a similar feature called "userData behavior" that allows you to persist data across multiple browser sessions.
DOM Storage is useful because no good browser-only methods exist for persisting reasonable amounts of data for any period of time. Browser cookies have limited capacity and provide no support for organizing persisted data, and other methods (such as Flash Local Storage) require an external plugin.
One of the first public applications to make use of the new DOM Storage functionality (in addition to Internet Explorer's userData Behavior) was halfnote (a note-taking application) written by Aaron Boodman. In his application, Aaron simultaneously saved notes back to a server (when Internet connectivity was available) and a local data store. This allowed the user to safely write backed-up notes even with sporadic Internet connectivity.
While the concept, and implementation, presented in halfnote was comparatively simple, its creation shows the possibility for a new breed of web applications that are usable both online and offline.
The following are global objects that exist as properties of every window object. This means that they can be accessed as sessionStorage or window.sessionStorage. (This is important because you can then use IFrames to store, or access, additional data, beyond what is immediately included in your page.)
StorageThis is a constructor (Storage) for all Storage instances (sessionStorage and globalStorage[location.host]). Setting Storage.prototype.removeKey = function(key){ this.removeItem(this.key(key)) } would be accessed as localStorage.removeKey and sessionStorage.removeKey.
globalStorage items are not an instance of Storage, but instead are an instance of StorageObsolete.
Storage is defined by the WhatWG Storage Interface as this:
interface Storage {
readonly attribute unsigned long length;
[IndexGetter] DOMString key(in unsigned long index);
[NameGetter] DOMString getItem(in DOMString key);
[NameSetter] void setItem(in DOMString key, in DOMString data);
[NameDeleter] void removeItem(in DOMString key);
void clear();
};
sessionStorageThis is a global object (sessionStorage) that maintains a storage area that's available for the duration of the page session. A page session lasts for as long as the browser is open and survives over page reloads and restores. Opening a page in a new tab or window will cause a new session to be initiated.
// Save data to a the current session's store sessionStorage.username = "John"; // Access some stored data alert( "username = " + sessionStorage.username );
The sessionStorage object is most useful for hanging on to temporary data that should be saved and restored if the browser is accidentally refreshed.
Examples:
Autosave the contents of a text field, and if the browser is accidentally refreshed, restore the text field contents, so that no writing is lost.
// Get the text field that we're going to track
var field = document.getElementById("field");
// See if we have an autosave value
// (this will only happen if the page is accidentally refreshed)
if ( sessionStorage.autosave ) {
// Restore the contents of the text field
field.value = sessionStorage.autosave;
}
// Check the contents of the text field every second
setInterval(function(){
// And save the results into the session storage object
sessionStorage.autosave = field.value;
}, 1000);
More information:
globalStorage
This is a global object (globalStorage) that maintains multiple private storage areas that can be used to hold data over a long period of time (e.g. over multiple pages and browser sessions).
globalStorage is not a Storage instance, but a StorageList instance containing StorageObsolete instances.// Save data that only scripts on the mozilla.org domain can access globalStorage['mozilla.org'].snippet = "<b>Hello</b>, how are you?";
Specifically, the globalStorage object provides access to a number of different storage objects into which data can be stored. For example, if we were to build a web page that used globalStorage on this domain (developer.mozilla.org) we'd have the following storage object available to us:
globalStorage['developer.mozilla.org'] - All web pages within the developer.mozilla.org sub-domain can both read and write data to this storage object.
Examples:
All of these examples require that you have a script inserted (with each of the following code) in every page that you want to see the result on.
Remember a user's username for the particular sub-domain that is being visited:
globalStorage['developer.mozilla.org'].username = "John";
Keep track of the number of times that a user visits all pages of your domain:
// parseInt must be used since all data is stored as a string
globalStorage['mozilla.org'].visits =
parseInt( globalStorage['mozilla.org'].visits || 0 ) + 1;
localStoragelocalStorage is the same as globalStorage[location.hostname], with the exception of being scoped to an HTML5 origin (scheme + hostname + non-standard port) and localStorage being an instance of Storage as opposed to globalStorage[location.hostname] being an instance of StorageObsolete. For example, http://example.com is not able to access the same localStorage object as https://example.com but they can access the same globalStorage item. localStorage is a standard interface while globalStorage is non-standard. localStorage was introduced in Firefox 3.5.
Please note that setting a property on globalStorage[location.hostname] does not set it on localStorage and extending Storage.prototype does not affect globalStorage items, only extending StorageObsolete.prototype does.
The DOM storage data is stored in the webappsstore.sqlite file in the profile folder.
See also clearing offline resources cache.
Page last modified 02:14, 13 Mar 2010 by Ehsan