The pagehide event is a browser event that fires when a page is being hidden or moved out of view, including when the browser is putting it into the back-forward cache. It is used for page lifecycle handling, state cleanup and persistence logic, and it is more reliable than unload for modern browser behaviour.
What the pagehide event does
The pagehide event marks a transition out of the current page view. Unlike unload, it is designed to work with modern browser lifecycle behaviour, including back-forward cache handling, so scripts can respond when the page is being hidden rather than assuming the document is permanently gone.
That distinction matters because a hidden page may be resumed later. Code that treats page exit as final can break state restoration, duplicate work, or lose pending changes when the browser preserves the page for a later return.
Where pagehide fits in the browser lifecycle
pagehide sits in the browser's page lifecycle, where a document may be suspended, cached, or replaced by another page. It is therefore a coordination point for cleanup logic that needs to run when visibility is ending, but before the browser fully discards the page context.
This makes it useful for logic that depends on lifecycle state rather than navigation intent. A page can be hidden because the user navigates away, switches tabs, or enters a history state that the browser may preserve for fast restoration.
Why pagehide is preferred over unload
Modern browsers increasingly restrict or ignore fragile exit-time behaviour, especially when pages participate in back-forward cache. The pagehide event is better aligned with that model because it lets developers observe the lifecycle transition without relying on a terminal unload path that browsers may skip or treat inconsistently.
For application code, that means cleaner separation between transient in-memory state and persisted state. It also reduces dependence on synchronous shutdown logic that can be slow, unreliable, or incompatible with browser caching optimisations.
Common implementation patterns
Typical uses include saving draft form data, pausing timers, releasing temporary UI state, flushing analytics or session metadata, and marking a page as no longer active. The right behaviour depends on whether the page is merely hidden, cached for later reuse, or truly leaving the session.
- Persist user state before it becomes unavailable.
- Avoid assuming the page will not return.
- Keep lifecycle handlers lightweight so they do not interfere with cache eligibility.
- Separate cleanup that is safe on hide from cleanup that should happen only on permanent teardown.
Standards & Framework Alignment
This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.
NIST SP 800-53 Rev 5, NIST CSF 2.0 and OWASP ASVS set the governance and control requirements practitioners need to meet.
| Framework | Control / Reference | Relevance |
|---|---|---|
| NIST SP 800-53 Rev 5 | AU-12 — Audit Record Generation | Pagehide handlers often flush or persist session activity before a page is cached or hidden. |
| Recommendation — Record lifecycle and session events when pages transition out of view. | ||
| NIST CSF 2.0 | PR.DS-11 — Data-at-rest is protected | Pagehide is commonly used to persist user state safely before browser suspension or cache reuse. |
| Recommendation — Protect locally stored state that pagehide may write before the page is suspended. | ||
| OWASP ASVS | V16 — Security Logging and Error Handling | Lifecycle handlers can affect event logging and graceful shutdown behaviour in web apps. |
| Recommendation — Verify that page lifecycle handlers log and fail safely when persistence work is interrupted. | ||
Related resources from NHI Mgmt Group
- What makes Shai Hulud 2.0 different from a normal npm malware event?
- What is the difference between quarterly certification and event-driven access control?
- When does event-driven IAM reduce risk more than periodic access reviews?
- When should organisations treat a successful login as a security event?
Deepen Your Knowledge
Free weekly newsletter
Subscribe to the NHI & AI Identity Journal
The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.
Bonus 33% off our NHI Course when you subscribe.
Reviewed and updated by the NHIMG editorial team on November 3, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org