logo Learn HTML
  • Home
  • Tutorials
  • Practice
  • Quiz
  • Projects
  • About
  • Contact

HTML Course

  • HTML HOME
  • HTML Introduction
  • HTML Basic
  • HTML Element
  • HTML Attributes
  • HTML Headings
  • HTML Paragrapha
  • HTML Style
  • HTML Formattin
  • HTML Quotations
  • HTML Colors
  • HTML Css
  • HTML Links
  • HTML Image
  • HTML Favicon
  • Page Tittle
  • HTML Tables
  • HTML Lists
  • Block & Inline
  • HTML Div
  • HTML Classes
  • HTML Id
  • HTML Buttons
  • HTML Iframes
  • HTML File Paths
  • HTML Head
  • HTML Layout
  • HTML Symbols
  • HTML Emojis
  • HTML URL Encode
  • HTML VS XHTML
  • HTML FROM
  • HTML Form
  • Form Attributes
  • Form Elements
  • HTML Input Types
  • HTML Input Attributes
  • Input Form Attributes
  • HTML Graphics
  • HTML Canvas
  • HTML SVG
  • HTML Media
  • HTML Media
  • HTML Video
  • HTML Audio
  • HTML Plug-ins
  • HTML Youtube
  • HTML APIS
  • HTML Web APIS
  • HTML Geolocation
  • HTML Drag & Drop
  • Web Storage
  • Web Workers
  • HTML SSE
  • HTML Examples
  • HTML Examples
  • HTML Editor
  • HTML Quiz
  • HTML Exercises
  • HTML References
  • HTML Tag List
  • HTML Attributes
  • HTML Global Attributes
  • HTML Brower Support
  • HTML Events
  • HTML Colors
  • HTML Canvas
  • HTML Audio & Video
  • HTML Doctypes
  • HTML Character Sets
  • HTML URL Encode
  • Language Codes
  • HTTP Messages
  • HTTP Methods
  • PX to EM Converter
  • keyboard Shortcuts

Understanding the HTML Web Storage API

Web Storage is a modern way for web applications to store data locally within a user's browser. It is significantly better than using cookies because it is more secure, allows for much larger amounts of data (at least 5MB), and—most importantly—the data is never sent to the server with every request, which keeps your website fast.


1. Key Features of Web Storage

  • Large Capacity: Store at least 5MB of data (cookies are limited to 4KB).

  • Security: Data stays in the browser and isn't exposed in HTTP headers.

  • Origin-Based:Data is tied to your specific domain and protocol.

  • Performance:Local storage doesn't slow down server requests.


2. The Two Storage Objects

The Web Storage API provides two distinct ways to save data, depending on how long you want that data to last.


3. Checking for Browser Support

Before using web storage, always check if the user's browser supports the API to prevent errors.


4. Working with localStorage

The localStorage object keeps data even after the browser is closed and reopened.


Code Example: Storing and Retrieving Preferences

Note:Data is always stored as strings. If you want to store numbers or objects, you must convert them (e.g., using Number() or JSON.stringify()).


5. Practical Example: Persistent Click Counter

This example uses localStorage to remember how many times a user clicked a button, even if they return days later.


6. Using sessionStorage

Use sessionStorage for data that should only last as long as the current tab is open. The syntax is exactly the same as localStorage.

Session Counter Example:


Summary Checklist

  • Capacity:Use Web Storage for data larger than 4KB.

  • Duration:localStorage for permanent data and sessionStorage for temporary data.

  • Formatting:Remember that everything is stored as a string!

Try It Your Self

Sign Up For New
Update

HTML Basic
  1. HTML Basic
  2. HTML Element
  3. HTML Attributes
  4. HTML Headings
  5. HTML Paragrapha
  6. HTML Tables
  7. HTML Lists
HTML Advance
  1. HTML Div
  2. HTML Classes
  3. HTML Id
  4. HTML Buttons
  5. HTML Iframes
  6. HTML Symbols
  7. HTML Emojis