The Page Visibility API is a new API that is supported by Chrome, Firefox, and Opera. Safari does not currently support the API.
The Page Visibility API is implemented as a property of the
window object. The visibility state of a tab can be determined by reading the visibility property of the window object. The visibility property is a
Boolean value that is
true if the tab is visible,
false if the tab is hidden, and
undefined if the tab is inactive.
The following code sample demonstrates how to use the Page Visibility API to determine the visibility state of a tab.
In the code sample, the
updateVisibility() function is called when the button is clicked. This function reads the visibility property of the window object and displays the text "Tab is visible" or "Tab is hidden" depending on the value of the visibility property.