This specification defines a means to programmatically determine the visibility state of a document. This can aid in the development of resource efficient web applications.

Page Visibility Level 2 replaces the first version of [[PAGE-VISIBILITY]] and includes:

The Working Group expects to demonstrate 2 implementations of the features listed in this specification by the end of the Candidate Recommendation phase. prerender is marked a feature at risk.

Introduction

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.

Examples of usage

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 intelligently 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.

Visibility states and the VisibilityState enum

The Document of the top level browsing context can be in one of the following visibility states:

hidden
The Document is not visible at all on any screen.
visible
The Document is at least partially visible on at least one screen. This is the same condition under which the hidden attribute is set to false.
prerender
The Document is loaded in the prerender mode and is not yet visible.

The visibility states are reflected in the API via the VisibilityState enum.

      enum VisibilityState {
        "hidden", "visible", "prerender"
      };

Extensions to the Document interface

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:

  1. If steps to determine the visibility state return visible, then return false.
  2. Otherwise, return 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:

  1. Let doc be the Document of the top level browsing context.
  2. If the defaultView of doc is null, return hidden.
  3. Otherwise, return the VisibilityState value that best matches the visibility state of doc:
    1. If doc was prerendered [[!RESOURCE-HINTS]] and has not previously transitioned to "visible", return "prerender".
    2. Return "visible" if:
      1. The user agent is not minimized and doc is the foreground tab.
      2. The user agent is fully obscured by an accessibility tool, like a magnifier, but a view of the doc is shown.
    3. Return "hidden" if:
      1. The user agent is minimized.
      2. The user agent is not minimized, but doc is on a background tab.
      3. The user agent is to unload doc.
      4. The Operating System lock screen is shown.

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.

Reacting to visibilitychange changes

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:

  1. Let doc be the Document of the top level browsing context.
  2. If doc is now visible:
    1. If traversing to a session history entry, run the now visible algorithm before running the step to fire the pageshow event.
    2. Otherwise, queue a task that runs the now visible algorithm.
  3. Else if doc is now not visible, or if the user agent is to unload doc:
    1. If the user agent is to unload the Document, run the now hidden algorithm during the unloading document visibility change steps.
    2. Otherwise, queue a task that runs the now hidden algorithm.

The now visible algorithm runs the following steps synchronously:

  1. Let doc be the Document of the top level browsing context.
  2. Fire a simple event named visibilitychange that bubbles, isn't cancelable, and has no default action, at the doc.

The now hidden algorithm runs the following steps synchronously:

  1. Let doc be the Document of the top level browsing context.
  2. Fire a simple event named visibilitychange that bubbles, isn't cancelable, and has no default action, at the doc.

Privacy and Security

The Page Visibility API enables developers to know when a Document is visible or in focus. Existing mechanisms, such as the focus and blur events, when attached to the Window object already provide a mechanism to detect when the Document is the active document; the unload event provides a notification that the page is being unloaded. This API extends these capabilities by also exposing the prerender state of the Document—see [[!RESOURCE-HINTS]] security and privacy section for relevant considerations and best practices on the use of prerender—and unifies all of the above in a single API to simplify development of visibility-aware and efficient applications.

Terminology

The following concepts and interfaces are defined in the [[!HTML51]] specification:

The [[!DOM4]] specification defines how to fire a simple event.

Acknowledgments

Thanks to Alex Komoroske, Arvind Jain, Boris Zbarsky, Cameron McCormack, James Robinson, Jason Weber, Jonas Sicking, Karen Anderson, Kyle Simpson, Nat Duca, Nic Jansma, Philippe Le Hegaret, and Todd Reifsteck for their contributions to this work.