What's New in HTML5
Web Storage: localStorage and sessionStorage
HTML5 introduces Web Storage, a simple and effective way to store data in the client's browser. This is achieved through two main technologies: localStorage
and sessionStorage
. Both allow key-value pair storage, but they differ in the lifespan of the stored data.
What is Web Storage?
Web Storage provides web applications with methods to persistently store data in the user's browser. Unlike cookies, Web Storage saves a large amount of data without affecting site performance. Also, unlike cookies, stored data is not automatically sent with every HTTP request.
localStorage
localStorage
allows storing data that persists even after closing the browser. The data remains until it is explicitly deleted.
Common methods of localStorage
setItem(key, value)
: Saves data under a specific key.getItem(key)
: Retrieves the value associated with a key.removeItem(key)
: Deletes a specific item by its key.clear()
: Deletes all stored items.key(index)
: Returns the key at a particular index.
Example of localStorage usage
html
[Placeholder Image: Diagram showing the lifecycle of localStorage, from initial data load to persistence after closing and reopening the browser]
sessionStorage
sessionStorage
stores data only during the browsing session. The data is deleted when the browser (or tab) is closed.
Common methods of sessionStorage
The methods are identical to those of localStorage
:
setItem(key, value)
: Saves data under a specific key.getItem(key)
: Retrieves the value associated with a key.removeItem(key)
: Deletes a specific item by its key.clear()
: Deletes all stored items.key(index)
: Returns the key at a particular index.
Example of sessionStorage usage
html
[Placeholder Image: Diagram showing the lifecycle of sessionStorage, from initial data load to deletion when the tab or browser is closed]
Differences between localStorage and sessionStorage
- Persistence:
localStorage
: Data persists indefinitely until explicitly deleted.sessionStorage
: Data persists only during the browsing session.
- Scope:
localStorage
: Accessible from any tab or window that loads the same origin.sessionStorage
: Only accessible within the tab or window where it was established.
[Placeholder Image: Visual comparison between localStorage and sessionStorage, highlighting their main differences in terms of persistence and scope]
Common use cases
-
localStorage:
- Save user preferences (dark/light theme, preferred language).
- Persist data between sessions (e.g., shopping cart).
-
sessionStorage:
- Store temporary data only needed during the current session.
- Maintain the state of a particular tab (e.g., form data until the process is completed).
Conclusion
Web Storage in HTML5, using localStorage
and sessionStorage
, provides an efficient and effective way to store data on the client side. The choice between one or the other will depend on the nature and persistence of the data you need to store in your web application.
- Introduction to HTML5
- Estructura básica de un documento HTML5
- New Semantic Elements
- Advanced Forms in HTML5
- Multimedia in HTML5: audio and video
- Graphics and Animations with Canvas and SVG
- Web Storage: localStorage and sessionStorage
- Geolocation API
- WebSockets API
- Web Workers API
- Drag and Drop in HTML5
- Accessibility Improvements with HTML5
- HTML5 and SEO
- HTML5 and CSS3: integrations and new possibilities
- Conclusion and Future Trends of HTML5