Document: visibilitychange event

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2021.

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.

The event is not cancelable.

Syntax

Use the event name in methods like addEventListener(), or set an event handler property.

js
addEventListener("visibilitychange", (event) => { })

onvisibilitychange = (event) => { }

Event type

A generic Event.

Usage notes

The event doesn't include the document's updated visibility status, but you can get that information from the document's visibilityState property.

This event fires with a visibilityState of hidden when a user navigates to a new page, switches tabs, closes the tab, minimizes or closes the browser, or, on mobile, switches from the browser to a different app. Transitioning to hidden is the last event that's reliably observable by the page, so developers should treat it as the likely end of the user's session (for example, for sending analytics data).

The transition to hidden is also a good point at which pages can stop making UI updates and stop any tasks that the user doesn't want to have running in the background.

Examples

Pausing music on transitioning to hidden

This example pauses playing audio when the page is hidden and resumes playing when the page becomes visible again. For a full example, see the Page Visibility API: Pausing audio on page hide documentation.

js
document.addEventListener("visibilitychange", () => {
  if (document.hidden) {
    playingOnHide = !audio.paused;
    audio.pause();
  } else if (playingOnHide) {
    // Resume playing if audio was "playing on hide"
    audio.play();
  }
});

Sending end-of-session analytics on transitioning to hidden

This example treats the transition to hidden as the end of the user's session, and sends the appropriate analytics using the Navigator.sendBeacon() API:

js
document.onvisibilitychange = () => {
  if (document.visibilityState === "hidden") {
    navigator.sendBeacon("/log", analyticsData);
  }
};

Specifications

Specification
HTML
# event-visibilitychange
HTML
# handler-onvisibilitychange

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
visibilitychange event
Chrome – Partial support
Chrome 33 – 61 (Release date: 2014-02-20)
footnote Partial support
footnote The onvisibilitychange event handler property is not supported.
Chrome – Partial support
Chrome 13 (Release date: 2011-08-02)
prefix
footnote Partial support
prefix Implemented with the vendor prefix: webkit
footnote The onvisibilitychange event handler property is not supported.
Chrome – Full support
Chrome 62 (Release date: 2017-10-17)
footnote Full support
Edge – Partial support
Edge 12 – 17 (Release date: 2015-07-29)
footnote Partial support
footnote The onvisibilitychange event handler property is not supported.
Edge – Full support
Edge 18 (Release date: 2018-10-02)
footnote Full support
Firefox – Full support
Firefox 56 (Release date: 2017-09-28)
footnote Full support
Opera – Partial support
Opera 12.1 – 12.1 (Release date: 2012-11-20)
footnote Partial support
footnote The onvisibilitychange event handler property is not supported.
Opera – Partial support
Opera 20 – 48 (Release date: 2014-03-04)
footnote Partial support
footnote The onvisibilitychange event handler property is not supported.
Opera – Partial support
Opera 15 (Release date: 2013-07-02)
prefix
footnote Partial support
prefix Implemented with the vendor prefix: webkit
footnote The onvisibilitychange event handler property is not supported.
Opera – Full support
Opera 49 (Release date: 2017-11-08)
footnote Full support
Safari – Partial support
Safari 7 – 10 (Release date: 2013-10-22)
footnote Partial support
footnote 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.
footnote Before Safari 14, the event does not bubble, so document.addEventListener('visibilitychange', ...) works, but window.addEventListener('visibilitychange', ...) does not.
footnote The onvisibilitychange event handler property is not supported.
Safari – Partial support
Safari 10.1 – 13.1 (Release date: 2017-03-27)
footnote Partial support
footnote 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.
footnote Before Safari 14, the event does not bubble, so document.addEventListener('visibilitychange', ...) works, but window.addEventListener('visibilitychange', ...) does not.
Safari – Partial support
Safari 14 – 14 (Release date: 2020-09-16)
footnote Partial support
footnote 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.
Safari – Full support
Safari 14.1 (Release date: 2021-04-26)
footnote Full support
Chrome Android – Partial support
Chrome Android 33 – 61 (Release date: 2014-02-26)
footnote Partial support
footnote The onvisibilitychange event handler property is not supported.
Chrome Android – Partial support
Chrome Android 18 (Release date: 2012-06-27)
prefix
footnote Partial support
prefix Implemented with the vendor prefix: webkit
footnote The onvisibilitychange event handler property is not supported.
Chrome Android – Full support
Chrome Android 62 (Release date: 2017-10-24)
footnote Full support
Firefox for Android – Full support
Firefox for Android 56 (Release date: 2017-09-28)
footnote Full support
Opera Android – Partial support
Opera Android 12.1 – 12.1 (Release date: 2012-10-09)
footnote Partial support
footnote The onvisibilitychange event handler property is not supported.
Opera Android – Partial support
Opera Android 20 – 45 (Release date: 2014-03-06)
footnote Partial support
footnote The onvisibilitychange event handler property is not supported.
Opera Android – Partial support
Opera Android 14 (Release date: 2013-05-21)
prefix
footnote Partial support
prefix Implemented with the vendor prefix: webkit
footnote The onvisibilitychange event handler property is not supported.
Opera Android – Full support
Opera Android 46 (Release date: 2018-05-14)
footnote Full support
Safari on iOS – Partial support
Safari on iOS 7 – 10 (Release date: 2013-09-18)
footnote Partial support
footnote 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.
footnote Before Safari on iOS 14, the event does not bubble, so document.addEventListener('visibilitychange', ...) works, but window.addEventListener('visibilitychange', ...) does not.
footnote The onvisibilitychange event handler property is not supported.
Safari on iOS – Partial support
Safari on iOS 10.3 – 13.4 (Release date: 2017-03-27)
footnote Partial support
footnote 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.
footnote Before Safari on iOS 14, the event does not bubble, so document.addEventListener('visibilitychange', ...) works, but window.addEventListener('visibilitychange', ...) does not.
Safari on iOS – Partial support
Safari on iOS 14 – 14 (Release date: 2020-09-16)
footnote Partial support
footnote 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.
Safari on iOS – Full support
Safari on iOS 14.5 (Release date: 2021-04-26)
footnote Full support
Samsung Internet – Partial support
Samsung Internet 2 – 7.4 (Release date: 2014-10-17)
footnote Partial support
footnote The onvisibilitychange event handler property is not supported.
Samsung Internet – Partial support
Samsung Internet 1 (Release date: 2013-04-27)
prefix
footnote Partial support
prefix Implemented with the vendor prefix: webkit
footnote The onvisibilitychange event handler property is not supported.
Samsung Internet – Full support
Samsung Internet 8 (Release date: 2018-07-18)
footnote Full support
WebView Android – Partial support
WebView Android 4.4.3 – 61 (Release date: 2014-06-02)
footnote Partial support
footnote The onvisibilitychange event handler property is not supported.
WebView Android – Partial support
WebView Android 4.4 (Release date: 2013-12-09)
prefix
footnote Partial support
prefix Implemented with the vendor prefix: webkit
footnote The onvisibilitychange event handler property is not supported.
WebView Android – Full support
WebView Android 62 (Release date: 2017-10-24)
footnote Full support
WebView on iOS – Partial support
WebView on iOS 7 – 10 (Release date: 2013-09-18)
footnote Partial support
footnote 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.
footnote Before WebView on iOS 14, the event does not bubble, so document.addEventListener('visibilitychange', ...) works, but window.addEventListener('visibilitychange', ...) does not.
footnote The onvisibilitychange event handler property is not supported.
WebView on iOS – Partial support
WebView on iOS 10.3 – 13.4 (Release date: 2017-03-27)
footnote Partial support
footnote 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.
footnote Before WebView on iOS 14, the event does not bubble, so document.addEventListener('visibilitychange', ...) works, but window.addEventListener('visibilitychange', ...) does not.
WebView on iOS – Partial support
WebView on iOS 14 – 14 (Release date: 2020-09-16)
footnote Partial support
footnote 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.
WebView on iOS – Full support
WebView on iOS 14.5 (Release date: 2021-04-26)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
Requires a vendor prefix or different name for use.
Has more compatibility info.

See also