Using the Page Visibility API

This is an experimental feature
Because this feature is still in development in some browsers, check the compatibility table for the proper prefixes to use in various browsers. Also note that the syntax and behavior of an experimental feature is subject to change in future version of browsers as the spec changes.

The Page Visibility API lets you know when a webpage is visible or in focus. With tabbed browsing, there is a reasonable chance that any given webpage is in the background and thus not visible to the user. When the user minimizes the webpage or moves to another tab, the API sends a change event regarding the visibility of the page. You can detect the event and perform some actions or behave differently. For example, if your web app is playing a video, it would pause the moment the user looks at another browser, and plays again when the user returns to the tab. The user does not lose her place in the video and can continue watching. For more examples of how you can use this API, see the Use cases section.  


The API is particularly useful for saving resources, 

Use cases

 A few examples:

  • A site has an image carousel that shouldn't advance to the next slide unless the user is viewing the page.
  • An application showing a dashboard of information doesn't want to poll the server for updates when the page isn't visible.
  • A page wants to detect when it is being prerendered so it can keep accurate count of page views.

Developers have historically used imperfect proxies to detect this. For example, registering an onblur/onfocus handler on the window helps you know when your page is not the active page, but it does not tell you that your page is hidden to the user. The Page Visibility API addresses this.


View live example (video with sound).

The example, which pauses the video when you switch to another tab and plays again when you return to its tab, was created with the following code:

// Set the name of the hidden property and the change event for visibility
var hidden, visibilityChange; 
if (typeof document.hidden !== "undefined") {
	hidden = "hidden";
	visibilityChange = "visibilitychange";
} else if (typeof document.mozHidden !== "undefined") {
	hidden = "mozHidden";
	visibilityChange = "mozvisibilitychange";
} else if (typeof document.msHidden !== "undefined") {
	hidden = "msHidden";
	visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
	hidden = "webkitHidden";
	visibilityChange = "webkitvisibilitychange";
var videoElement = document.getElementById("videoElement");

// If the page is hidden, pause the video;
// if the page is shown, play the video
function handleVisibilityChange() {
	if (document[hidden]) {
	} else {;

// Warn if the browser doesn't support addEventListener or the Page Visibility API
if (typeof document.addEventListener === "undefined" || 
	typeof hidden === "undefined") {
	alert("This demo requires a browser, such as Google Chrome or Firefox, that supports the Page Visibility API.");
} else {

    // Handle page visibility change   
    document.addEventListener(visibilityChange, handleVisibilityChange, false);
    // Revert to the existing favicon for the site when the page is closed;
    // otherwise the favicon remains paused.png
    window.addEventListener("unload", function(){
	}, false);
    // When the video pauses, set the favicon.
    // This shows the paused.png image
    videoElement.addEventListener("pause", function(){
	}, false);
    // When the video plays, set the favicon.
    videoElement.addEventListener("play", function(){
	}, false);
    // set the document (tab) title from the current video time
    videoElement.addEventListener("timeupdate", function(){
		document.title = Math.floor(videoElement.currentTime) + " second(s)";
	}, false);




Method overview





Returns true if the page is in a state considered to be hidden to the user, and false otherwise.


Returns a string denoting the visibility state of the document. Possible values:

  • visible : the page content may be at least partially visible. In practice this means that the page is the foreground tab of a non-minimized window. 
  • hidden : the page content is not visible to the user. In practice this means that the document is either a background tab or part of a minimized window.
  • prerender : the page content is being prerendered and is not visible to the user (considered hidden for purposes of document.webkitVisible).  The document may start in this state, but will never transition to it from another value.
//startSimulation and pauseSimulation defined elsewhere
function handleVisibilityChange() {
    if (document.hidden) {
    } else  {
document.addEventListener("visibilitychange", handleVisibilityChange, false);
<meta charset="utf-8"/>


Visibility states of an iframe is as same as the parent document. Hiding the iframe with CSS properties does not trigger visibility events nor change the state of the content document.


Specification Status Comment
W3C Page Visibility API Working Draft  

Browser compatibility

  • Desktop
  • Mobile

Feature Chrome (Webkit) Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support

13 (?) [1] webkit

10 (10) [2] moz 10 [3] ms -- -- (?)
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support ? ? ? ? ?

See also

Tags (3)

Edit tags

Attachments (0)


Attach file