Document.onvisibilitychange

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

Safari doesn’t fire the visibilitychange event as expected when visibilityState transitions to hidden; so for that case, you need to also include code to listen for the pagehide event.

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

Specification Status Comment
Page Visibility (Second Edition)
The definition of 'onvisibilitychange' in that specification.
Recommendation Initial definition.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
onvisibilitychangeChrome Full support 33
Full support 33
Full support 13
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge Full support 18
Notes
Full support 18
Notes
Notes 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() {});.
Firefox Full support 56IE No support No
Notes
No support No
Notes
Notes 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() {});.
Opera Full support 12.1
Notes
Full support 12.1
Notes
Notes Doesn't fire the visibilitychange event when the browser window is minimized, nor when hidden is set to true.
Safari Partial support 10.1
Notes
Partial support 10.1
Notes
Notes 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.
Notes 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() {});.
WebView Android Full support 4.4.3Chrome Android Full support 33Firefox Android Full support 56Opera Android Full support 12.1
Notes
Full support 12.1
Notes
Notes Doesn't fire the visibilitychange event when the browser window is minimized, nor when hidden is set to true.
Safari iOS Partial support 10.3
Notes
Partial support 10.3
Notes
Notes 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.
Notes 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() {});.
Samsung Internet Android Full support 2.0

Legend

Full support  
Full support
Partial support  
Partial support
No support  
No support
See implementation notes.
See implementation notes.
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.

See also