Browser troubleshooting

Choose between localStorage and sessionStorage

Use localStorage for small origin-scoped values that may persist across sessions, and sessionStorage for values scoped to a page session. Both APIs store strings and can throw when storage is unavailable.

Worked example

Browser console example · use a disposable page on your own site

try {
  sessionStorage.setItem("guide-demo", JSON.stringify({page: 2}));
  console.log(JSON.parse(sessionStorage.getItem("guide-demo")).page);
  sessionStorage.removeItem("guide-demo");
} catch (error) { console.log("Storage unavailable:", error.name); }

How it works

The example writes one namespaced demo key and removes it afterward. JSON serialization preserves a small structured value within string-based storage. Session storage is useful for transient UI state that should not become a long-term saved record.

Limits and mistakes

Neither storage type is a secure vault for secrets from same-origin scripts. LocalStorage persistence is best-effort and user-controlled. Storage APIs are synchronous, so large reads and writes can block the main thread.

Check the result

Expect 2 when storage is permitted. Test a reload and a separate tab in the target browser to confirm the intended session behavior, and keep the unavailable-storage path usable.

Sources

Reference documentation for the behavior used in this guide: