In JavaScript, the visibilitychange event fires on the document when its visibility status changes (MDN)—for example when the user switches tabs, navigates away, minimizes the browser, or switches apps on mobile.
Register a listener with addEventListener. Check readiness and browser support before you rely on this event.
Syntax
Listen for visibilitychange. Start with this line.
JavaScript
addEventListener("visibilitychange", …)
Call
What it does
addEventListener("visibilitychange", …)
Primary signature.
Built into the browser. Listen with document.addEventListener("visibilitychange", …) — no import required.
Library declarations
JavaScript
addEventListener("visibilitychange", …)
Parameters
Parameter
Description
Listener
Required. addEventListener or onvisibilitychange
visibilityState / hidden
Required. Current visibility after the change
Event object
Optional. Generic Event — no state fields
Return Value
Return Value
Description
*
Fires as a Document event (listener receives the Event object).
JavaScript Examples
Example 1 — Listen and Read visibilityState
Log the new state whenever visibility changes (MDN pattern).
JavaScript
document.addEventListener("visibilitychange", () => {
console.log(document.visibilityState);
// "visible" or "hidden"
});
Gate on hidden. Only send when visibilityState === "hidden".
Use sendBeacon. Designed to finish sending even as the page unloads.
Why here? MDN: transitioning to hidden is often the last reliable signal.
Prefer over unload alone.unload / beforeunload are less reliable for analytics.
Compatibility
Browser Support
Document visibilitychange is part of the Page Visibility API. MDN marks it Baseline Widely available since April 2021 — safe for everyday use in modern browsers.
✓ Baseline Widely available
Document visibilitychange
Fires when the document becomes visible or hidden — read visibilityState or hidden inside the handler.
WideBaseline
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
Android WebViewSupported · Modern WebView
Full support
visibilitychangeWidely available
Bottom line: Listen on document, read visibilityState/hidden, pause work when hidden, and prefer sendBeacon for end-of-session analytics.
Frequently Asked Questions
It fires on the document when visibility status changes — for example switching tabs, navigating away, minimizing the browser, or switching apps on mobile (MDN).
No. MDN marks Document visibilitychange as Baseline Widely available (since April 2021). It is not Deprecated, Experimental, or Non-standard.
Read document.visibilityState ("visible" or "hidden") or document.hidden. The Event object does not include the new state (MDN).
No. MDN states visibilitychange is not cancelable.
Transitioning to hidden is often the last reliably observable event. MDN recommends sending end-of-session data then (for example with navigator.sendBeacon).
Yes. Use document.onvisibilitychange or document.addEventListener("visibilitychange", …).