Document.onvisibilitychange

The Document.onvisibilitychange property represents the event handler that is called when a visibilitychange event reaches this object.

Syntax

obj.onvisibilitychange = function;
  • function is the name of a user-defined function, without the () suffix or any parameters, or an anonymous function declaration.

Example

document.onvisibilitychange = function() {
  console.log("Visibility of page has changed!");
};

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
onvisibilitychange
62
Before Chrome 62, this event handler attribute is not supported; however, the event itself is supported since Chrome 33. The event can be listened to via document.addEventListener('visibilitychange', function() {});.
18
Before Edge 18, this event handler attribute was not supported; however, the event itself was supported since Edge 12. The event can be listened to via document.addEventListener('visibilitychange', function() {});.
56
No
This event handler attribute is not supported; however, the event itself is supported since IE 10. The event can be listened to via document.addEventListener('visibilitychange', function() {});.
49
Before Opera 49, this event handler attribute is not supported; however, the event itself is supported since Opera 20. The event can be listened to via document.addEventListener('visibilitychange', function() {});.
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 WebKit bugs 116769, 151234, 151610, and 194897.", "Before Safari 10.1, this event handler attribute was not supported; however, the event itself was supported since Safari 7. The event can be listened to via document.addEventListener('visibilitychange', function() {});."]
62
Before WebView 62, this event handler attribute is not supported; however, the event itself is supported since WebView 4.4.3. The event can be listened to via document.addEventListener('visibilitychange', function() {});.
62
Before Chrome 62, this event handler attribute is not supported; however, the event itself is supported since Chrome 33. The event can be listened to via document.addEventListener('visibilitychange', function() {});.
56
46
Before Opera Android 46, this event handler attribute is not supported; however, the event itself is supported since Opera Android 20. The event can be listened to via document.addEventListener('visibilitychange', function() {});.
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 WebKit bugs 116769, 151234, 151610, and 194897.", "Before Safari iOS 10.3, this event handler attribute was not supported; however, the event itself was supported since Safari iOS 7. The event can be listened to via document.addEventListener('visibilitychange', function() {});."]
8.0
Before Samsung Internet 8.0, this event handler attribute is not supported; however, the event itself is supported since Samsung Internet 2.0. The event can be listened to via document.addEventListener('visibilitychange', function() {});.

See also

© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Document/onvisibilitychange