The Document visibilitychange event fires when the page’s visibility status changes—for example when the user switches tabs, minimizes the browser, or (on mobile) leaves the app. Learn to read document.visibilityState and document.hidden, and practice with five try-it labs.
01
Kind
Document event
02
Type
Event (generic)
03
Cancelable
No
04
Read
visibilityState
05
API
Page Visibility
06
Status
Baseline · Widely available
Fundamentals
Introduction
A page can be open but not what the user is looking at—another tab is focused, the window is minimized, or the phone switched to a different app. The Page Visibility API reports that with document.visibilityState and document.hidden.
Whenever that status changes, Document visibilitychange fires. The Event object itself does not include the new state; you read document.visibilityState (or document.hidden) inside the handler.
💡
Beginner tip
Transitioning to hidden is often the last reliable signal before a tab goes away. MDN recommends treating it as a likely end of the session for analytics (for example with navigator.sendBeacon)—prefer this over relying only on unload / beforeunload.
Concept
Understanding Document visibilitychange
A standard Document event that answers: “Did this page just become visible or hidden to the user?”
Fires when the document’s visibility status changes (MDN).
Triggers include switching tabs, navigating away, minimizing/closing the browser, or switching apps on mobile.
Not cancelable.
Event type — a generic Event.
Handler — document.onvisibilitychange or addEventListener("visibilitychange", ...).
Status — Baseline Widely available since April 2021 (MDN).
Foundation
📝 Syntax
Use the event name with addEventListener, or set the handler property:
document.addEventListener("visibilitychange", () => {
console.log(document.visibilityState); // "visible" or "hidden"
console.log(document.hidden); // true when hidden
});
visibilityState values (beginner view)
Value
Meaning
visible
The page content may be visible to the user
hidden
The page is not visible (background tab, minimized, etc.)
Compare
⚖️ visibilitychange vs unload / blur
Topic
visibilitychange
unload / beforeunload
Window blur
Means
Page visibility changed
Page may be leaving / unloading
Window lost focus
Tab switch
Yes — becomes hidden
Often unreliable
May fire, but not the Page Visibility API
Read state
visibilityState / hidden
N/A
Not the same as visibility
Analytics tip
Prefer hidden + sendBeacon
Avoid as only signal
Not ideal for session end
MDN guidance
Page Visibility API
Less reliable for modern browsers
Focus, not visibility
Cheat Sheet
⚡ Quick Reference
Goal
Code / note
Listen
document.addEventListener("visibilitychange", fn)
Handler property
document.onvisibilitychange = fn
Current state
document.visibilityState
Boolean helper
document.hidden (true when not visible)
Pause work when hidden
if (document.hidden) { /* pause */ }
End-of-session log
On hidden, call navigator.sendBeacon(...)
MDN status
Baseline Widely available (Apr 2021)
Snapshot
🔍 At a Glance
Four facts to remember about Document visibilitychange.
Event type
Event
No state on event
Means
Visibility changed
Tab / app focus
Read with
visibilityState
Or document.hidden
Baseline
yes
Widely available
Hands-On
Examples Gallery
Examples follow MDN Document: visibilitychange event. In try-it labs, switch away from the tab (or minimize) then come back to see hidden ↔ visible updates.
📚 Getting Started
Log visibility changes with both listener styles.
Example 1 — Log visibilityState
Print the current state whenever visibility changes.
Document visibilitychange is marked Baseline Widely available on MDN (since April 2021). Logos use the shared browser-image-sprite.png sprite from this project. Pair it with document.visibilityState and document.hidden.
✓ Baseline · Widely available
Document visibilitychange
Fires when the document visibility status changes. Read visibilityState or hidden inside the handler.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium Edge
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerLegacy Page Visibility support in older IE; prefer modern browsers
Legacy
visibilitychangeExcellent
Bottom line: Listen on document for visibilitychange, read visibilityState/hidden, pause background work when hidden, and use sendBeacon for leave analytics.
Wrap Up
Conclusion
Document visibilitychange is the Page Visibility signal that the user left or returned to your page. Read document.visibilityState (or document.hidden) in the handler to pause work, save state, or send analytics.
It fires when the document visibility status changes—for example when the user switches tabs, navigates away, minimizes the browser, or switches apps on mobile.
Is visibilitychange deprecated or experimental?
No. MDN marks Document visibilitychange as Baseline Widely available (since April 2021). It is not Deprecated, Experimental, or Non-standard.
How do I know if the page is hidden?
Inside the handler, read document.visibilityState ("visible" or "hidden") or document.hidden (true when not visible). The Event object does not include the new state.
Is the event cancelable?
No. MDN states visibilitychange is not cancelable.
Why use it instead of unload?
Transitioning to hidden is often the last reliably observable event. MDN recommends it for end-of-session analytics (for example with navigator.sendBeacon) instead of relying only on unload/beforeunload.
Is there an onvisibilitychange property?
Yes. You can use document.onvisibilitychange or document.addEventListener("visibilitychange", ...).
Did you know?
Transitioning to hidden is often the last event that is reliably observable by the page—which is why MDN recommends it for end-of-session analytics instead of depending only on unload.