Page visibility
The Document.hidden read-only property returns a Boolean value indicating if the page is considered hidden or not. This can be used to check whether the document is in the background or in a minimized window, or is otherwise not visible to the user.
The Document.visibilityState property provides an alternative way to determine whether the page is hidden.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| The visibilitychange event is fired at the document when its visibility status changes — for example, when the user switches browser tabs, navigates to a new page, minimizes or closes the browser, or on mobile, switches to a different app. | 62 | 18 | 56 | 14.1 | 62 | 14.5 |
| The Document.visibilityState read-only property returns the visibility of the document. It can be used to check whether the document is in the background or in a minimized window, or is otherwise not visible to the user. | 33 | 12 | 18 | 7 | 33 | 7 |
- Available with a vendor prefix: webkit (13)
- Since Firefox 56 it also returns `true` on macOS when the window is completely hidden by another non-translucent application.
- This feature was removed in a later browser version (52)
- Available with a vendor prefix: moz (10)
- Available with a vendor prefix: webkit (18)
- This browser only partially implements this feature
- This feature was removed in a later browser version (62)
- The `onvisibilitychange` event handler property is not supported.
- Available with a vendor prefix: webkit (13)
- This browser only partially implements this feature
- This feature was removed in a later browser version (18)
- The `onvisibilitychange` event handler property is not supported.
- This browser only partially implements this feature
- This feature was removed in a later browser version (14.1)
- This feature was removed in a later browser version (14)
- This feature was removed in a later browser version (10.1)
- Doesn't fire the `visibilitychange` event when navigating away from a document, so also include code to check for the `pagehide` event (which does fire for that case in all current browsers). See bug 116769, bug 151234, bug 151610, and bug 194897.
- Before Safari 14, the event does not bubble, so `document.addEventListener('visibilitychange', ...)` works, but `window.addEventListener('visibilitychange', ...)` does not.
- The `onvisibilitychange` event handler property is not supported.
- This browser only partially implements this feature
- This feature was removed in a later browser version (62)
- The `onvisibilitychange` event handler property is not supported.
- Available with a vendor prefix: webkit (18)
- This browser only partially implements this feature
- This feature was removed in a later browser version (14.5)
- This feature was removed in a later browser version (14)
- This feature was removed in a later browser version (10.3)
- Doesn't fire the `visibilitychange` event when navigating away from a document, so also include code to check for the `pagehide` event (which does fire for that case in all current browsers). See bug 116769, bug 151234, bug 151610, and bug 194897.
- Before Safari on iOS 14, the event does not bubble, so `document.addEventListener('visibilitychange', ...)` works, but `window.addEventListener('visibilitychange', ...)` does not.
- The `onvisibilitychange` event handler property is not supported.
- Available with a vendor prefix: webkit (13)
- This feature was removed in a later browser version (52)
- Available with a vendor prefix: moz (10)
- Available with a vendor prefix: webkit (18)
Syntax
document.addEventListener("visibilitychange", () => {
console.log(document.hidden);
// Modify behavior…
}); Use cases
-
Using Page visibility
The Document.
Cautions
- May not be supported in older browsers.
Implementation notes
- Some APIs require a secure context (HTTPS) or user activation. Check the requirements before use.