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
Unknown
The definition of 'onvisibilitychange' in that specification.
Recommendation Initial definition.

Browser compatibility

BCD tables only load in the browser

See also