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.


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


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


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

Browser compatibility

BCD tables only load in the browser

See also