This specification defines a means to programmatically determine the visibility state of a document. This can aid in the development of power and CPU efficient web applications.
This is a work in progress and may change without any notices.
The Page Visibility API defines a means to programmatically determine the visibility state of a top level browsing context, and to be notified if the visibility state changes. Without knowing the visibility state of a page, web developers have been designing web pages as if they are always visible. This not only results in higher machine resource utilization, but it prevents web developers from making runtime decisions based on whether the web page is visible to the user. Designing web pages with knowledge of the page's visibility state can result in improved user experiences and power efficient sites.
With this API, web applications can choose to alter their behavior based on whether they are visible to the user or not. For example, this API can be used to scale back work when the page is no longer visible.
To improve the user experience and optimize CPU and power efficiency the application could autoplay a video when the application is visible, and automatically pause the playback when the application is hidden:
var videoElement = document.getElementById("videoElement");
// pause video buffering if page is being prerendered
if (document.visibilityState == "prerender") {
// ...
}
// Autoplay the video if application is visible
if (document.visibilityState == "visible") {
videoElement.play();
}
// Handle page visibility change events
function handleVisibilityChange() {
if (document.visibilityState == "hidden") {
videoElement.pause();
} else {
videoElement.play();
}
}
document.addEventListener('visibilitychange', handleVisibilityChange, false);
Similar logic can be applied to intellegently pause and resume, or
throttle, execution of application code such as animation loops,
analytics, and other types of processing. By combining the
visibilityState attribute of the Document interface
and the visibilitychange event, the application is able to
both query and listen to page visibility events to deliver a better
user experience, as well as improve efficiency and performance of its
execution.
VisibilityState enum
The Document of the top level browsing context can be in one of the following visibility states:
hidden attribute is set to false.
The visibility states are reflected in the API via the VisibilityState enum.
enum VisibilityState {
"hidden", "visible", "prerender"
};
This specification extends the [[!HTML51]] Document interface:
partial interface Document {
readonly attribute boolean hidden;
readonly attribute VisibilityState visibilityState;
attribute EventHandler onvisibilitychange;
};
hidden attribute
On getting, the hidden attribute MUST run the steps to determine if the document is hidden:
visible, then return false.
true.
Support for hidden attribute is maintained for
historical reasons. Developers should use
visibilityState where possible.
visibilityState attribute
On getting, the visibilityState attribute the user agent MUST run the steps to determine the visibility state:
Document of the top
level browsing context.
defaultView of doc is
null, return hidden.
To accommodate assistive technologies that are typically full screen
but still show a view of the page, when applicable, on getting, the
visibilityState attribute MAY return
visible, instead of hidden, when the user
agent is not minimized but is fully obscured by other applications.
onvisiblitychange event handler
onvisibilitychange is an event handler IDL attribute for the visibilitychange event type.
The task source for these tasks is the user interaction task source.
When the user agent determines that the visibility of the Document of the top level browsing context has changed, the user agent MUST run the following steps:
pageshow event.
The now visible algorithm runs the following steps synchronously:
visibilitychange that
bubbles, isn't cancelable, and has no default action, at the
doc.
The now hidden algorithm runs the following steps synchronously:
visibilitychange that
bubbles, isn't cancelable, and has no default action, at the
doc.
The Page Visibility API enables third party content on a web page to determine the visibility of the Document contained by the top level browsing context with higher precision compared to existing mechanisms, like focus or blur events. However, for practical considerations, the additional exposure is not substantial.
The following concepts and interfaces are defined in the [[!HTML51]] specification:
defaultView
pageshow
Document
blur
focus
The [[!DOM4]] specification defines how to fire a simple event.
We would like to sincerely thank Karen Anderson, Nic Jansma, Alex Komoroske, Cameron McCormack, James Robinson, Jonas Sicking, Kyle Simpson, Jason Weber, and Boris Zbarsky to acknowledge their contributions to this work.
The following where changes since Page Visibility (Second Edition) 29 October 2013:
VisibilityState.unloaded has been removed.