HTML Web Storage
Web storage in HTML; preferable to cookies.
What is HTML Web Storage?
Web apps can store data locally in the user’s browser by using web storage.
Application data had to be provided in each server request and kept in cookies prior to HTML5. Significant volumes of data can be locally stored on websites without degrading their performance, and web storage offers greater security.
The storage limit is much higher (at least 5MB), and data is never sent to the server—in contrast to cookies.
Web storage is based on origin (i.e., protocol and domain). The same data can be stored and accessed by all pages from a single origin.
Browser Support
The first browser version that fully supports Web Storage is shown by the numbers in the table.
API | |||||
---|---|---|---|---|---|
Web Storage | 4.0 | 8.0 | 3.5 | 4.0 | 11.5 |
HTML Web Storage Objects
There are two objects available for client data storage with HTML web storage:
- window.localStorage: holds information without a time limit
- Data is stored for a single session in window.sessionStorage; when the browser tab is closed, the data is destroyed.
Verify that your browser supports localStorage and sessionStorage before utilizing web storage:
CODE MUKVO
The localStorage Object
The data is stored in the localStorage object without an expiration date. When the browser is closed, the data is not erased; it remains accessible for use the following day, week, or year.
Example
CODE MUKVO
Example explained:
Use name=”lastname” and value=”Smith” to create a localStorage name/value combination.
Get “lastname”‘s value and add it to the element with id=”result”
It is also possible to write the previous example like this:
CODE MUKVO
The syntax for removing the “lastname” localStorage item is as follows:
CODE MUKVO
Note: Strings are used to hold name/value pairs. Don’t forget to change them into a different format as needed!
The example that follows keeps track of how many times a user has pressed a button. To increase the counter, this code converts the value string to a number:
Example
CODE MUKVO
The sessionStorage Object
With the exception of storing data for a single session, the sessionStorage object is identical to the localStorage object. When a user exits a particular browser tab, the data is removed.
The sample that follows keeps track of how many times a user has clicked a button during the current session:
Example
CODE MUKVO
HTML web storage
localStorage
sessionStorage
cookies
client-side data storage
data persistence
browser storage
web application data
offline data
website performance
data securityHTML
HTML5
HTML tutorials
Learn HTML
Free HTML tutorials
HTML Example
HTML Explained
HTML Web Storage: Empowering Client-Side Data Persistence
In the ever-evolving world of web development, the ability to store and retrieve data efficiently is crucial. HTML web storage offers a robust solution, providing developers with the tools to enhance the user experience and optimize website performance.
The two primary mechanisms of HTML web storage are localStorage and sessionStorage. localStorage allows data to be stored with no expiration date, persisting even after the browser is closed and reopened. sessionStorage, on the other hand, stores data for the current session only, ensuring the information is cleared when the browser tab or window is closed.
In contrast to traditional cookies, which have size limitations and are sent with every HTTP request, HTML web storage offers a more flexible and scalable approach to client-side data storage. Developers can leverage these technologies to store user preferences, session information, and other relevant data, reducing the need for server-side processing and improving overall website performance.
Furthermore, HTML web storage provides a secure way to handle sensitive information, as the data is stored locally within the user’s browser, rather than being transmitted to the server with each request. This enhances data security and privacy, making it a valuable tool for building robust and trustworthy web applications.
As the demand for seamless, responsive, and personalized web experiences continues to grow, the importance of HTML web storage cannot be overstated. By mastering these client-side data storage techniques, developers can create more efficient, user-friendly, and secure web applications that cater to the evolving needs of modern users.