Chuck's Academy

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

  1. setItem(key, value): Saves data under a specific key.
  2. getItem(key): Retrieves the value associated with a key.
  3. removeItem(key): Deletes a specific item by its key.
  4. clear(): Deletes all stored items.
  5. 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:

  1. setItem(key, value): Saves data under a specific key.
  2. getItem(key): Retrieves the value associated with a key.
  3. removeItem(key): Deletes a specific item by its key.
  4. clear(): Deletes all stored items.
  5. 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.


Ask me anything