HTML5 Web Storage
HTML5 Web Storage, also known as DOM Storage, is a way to preserve state on either the client or server, making it much easier to work against the stateless nature of HTTP.
Browser Support
Web storage is supported in all major browsers such as Internet Explorer 8+, Firefox, Opera, Chrome, and Safari.
Note: Internet Explorer 7 and earlier versions, do not support web storage.
Web Storage Strengths and Weaknesses
Strengths
- Apps can work both online and offline.
- An easy API to learn and use.
- Has the ability to hook into browser events, such as offline, online, storage change.
- It has less overhead than cookies; no extra header data is sent with browser requests.
- It provides more space than cookies so increasingly complex information can be kept.
Weaknesses
- Data is stored as a simple string; manipulation is needed to store objects of differing types, such as booleans, objects, ints, and floats.
- It has a default 5MB limit; more storage can be allowed by the user if required.
- It can be disabled by the user or systems administrator.
- Storage can be slow with complex sets of data.
HTML5 Web Storage Methods
- setItem(key,value): adds a key/value pair to the sessionStorage object.
- getItem(key): retrieves the value for a given key.
- clear(): removes all key/value pairs for the sessionStorage object.
- removeItem(key): removes a key/value pair from the sessionStorage object.
- key(n): retrieves the value for a key.
Setting a Key/Value
There are two different methods for setting the information in sessionStorage:
- sessionStorage.setItem('someKey','someValue');
- sessionStorage.someKey = 'someValue';
Getting a Value
There are two methods to retrieve a key/value pair as well:
- sessionStorage.getItem('someKey'); //returns 'someValue'
- sessionStorage.someKey; //returns 'someValue'
Removing a Key/Value
What we need to do is to provide the key to the removeItem method, as in the following:
- sessionStorage.removeItem('someKey');//returns 'undefined' for someKey
- Clearing Storage
- sessionStorage.clear(); //everything gone!
HTML5 Web Storage Properties
HTML5 Web Storage has the following properties:
- Constructor Returns a reference to the constructor of an object.
- Length Retrieves the length of the key/value list.
- RemainingSpace Retrieves the remaining memory space, in bytes, for the storage object.
Advantages of Web Storage in comparison to Cookies
Web storage can be viewed simplistically as an improvement of cookies.
- With HTML5, web pages can store data locally within the user's browser. Earlier, this was done with cookies. However, Web Storage is more secure and faster.
- Like cookies, Web developers can store per-session or domain-specific data as name/value pairs on the client using Web Storage. However, unlike cookies, Web Storage makes it easier to control how information stored by one window is visible to another.
- , Unlike cookies, data stored in Web Storage remains on the client and is never transferred to the server. Cookies, in contrast, are sent back and forth between the browser and the server with each HTTP request. In other words, in Web Storage, the data never leaves the browser.
- Web Storage also offers significantly more disk space than cookies. Cookies can store only 4 kilobytes (KB) of data. This byte total can be one name/value pair of 4 KB, or it can be up to 20 name/value pairs that have a total size of 4 KB recommending a minimum of 4096 bytes per cookie. By comparison, Web Storage provides roughly 10 megabytes (MB) for each storage area.
- Functionally, client storage areas are quite different from cookies. Web Storage doesn't transmit values to the server with every request as cookies do, nor does the data in a local storage area ever expire. And unlike cookies, it is easy to access individual pieces of data.
- The side effect of cookies is that they are always sent with every HTTP request resulting in more data being sent over the wire.
The two types of Web Storage Objects to store the data is:
- localStorage
- sessionStorage
Web Storage data is, in both cases, not available between different browsers. For example, storage objects created in Firefox cannot be accessed in Internet Explorer, exactly like cookies.
You can add data to either server (session) storage or a local (client memory) storage by using Web Storage.
localStorage
localStorage is kept even among browser sessions. It is used to store the data locally. It stores the data with no expiration date, in other words if the browser is closed then it will not delete the data and we can view the data any time and even after a year.
- Long term data persistence.
- The local storage object spans multiple windows and persists beyond the current session.
- Values put into localStorage are shared across all windows from the same origin.
- Data is stored on the client. Behaving similarly to cookies, values persist beyond when the browser's session ends.
Example of localStorage



Jin NecesarioPosted Aug 17, 2019, 7:32 PM
Nice article.