# Console: SecurityError: Failed to read the 'localStorage' property from 'Window': Access is denied for this document

**Code:** `SecurityError: Failed to read the 'localStorage' property` · **Category:** Browser · **Seen on:** Chrome, Firefox, Safari, JavaScript

The page cannot use `localStorage` because cookies/site data are blocked, it runs in a sandboxed iframe, or the browser is in a restricted mode.

## Meaning

Browsers deny web storage when "Block all cookies" is set, for third-party iframes with blocked storage, for sandboxed iframes without `allow-same-origin`, in some private modes, and for `file://` or opaque origins (`data:` URLs). Wrap storage access in `try/catch` and fall back to memory.

## Common causes

- "Block third-party cookies" or all site data enabled
- Embedded in a cross-site iframe (storage partitioned/blocked)
- Sandboxed iframe lacking `allow-same-origin`
- Privacy mode/extension blocking storage
- Opaque origin (about:blank in a sandbox, data: URL)

## Quick fix

1. Guard storage access with `try/catch` and provide a fallback
2. For iframes add `allow-same-origin` (only if safe) or use the Storage Access API
3. Ask users to allow site data for your domain

## Detailed fix by platform

### JavaScript

1. `function store() { try { localStorage.setItem('k','v'); return localStorage; } catch (e) { return new Map(); } }`
2. `document.requestStorageAccess()` in third-party iframes after a user gesture

## Code examples

### Safe storage wrapper

```javascript
const safeStorage = (() => {
  try {
    const k = '__t'; localStorage.setItem(k, '1'); localStorage.removeItem(k);
    return localStorage;
  } catch (e) {
    const mem = new Map();
    return { getItem: k => mem.get(k) ?? null, setItem: (k, v) => mem.set(k, String(v)), removeItem: k => mem.delete(k) };
  }
})();
```

## How to diagnose

1. **Context** — Top-level page or iframe?
2. **Settings** — Cookies/site data blocked?
3. **Sandbox** — iframe sandbox attribute?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/browser/securityerror-localstorage-access-denied)
