Using the Page Visibility API

  • Revision slug: DOM/Using_the_Page_Visibility_API
  • Revision title: Using the Page Visibility API
  • Revision id: 26377
  • Created:
  • Creator: ernestd
  • Is current revision? No
  • Comment 8 words removed

Revision Content

With tabbed browsing, there’s a reasonable chance that any given web page is in the background and thus not visible to the user. Some sites might want to behave differently in such a case. 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 pageviews

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.

document.webkitHidden

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

document.webkitVisibilityState

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.webkitHidden) {
pauseSimulation();
} else {
startSimulation();
}
}
document.addEventListener("webkitvisibilitychange", handleVisibilityChange, false);
<meta charset="utf-8"/>

Browser compatibility

{{ CompatibilityTable() }}

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

13

{{ CompatNo() }}

 

{{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}

Specification

Described in the W3C Working Draft: Page Visibility

See also

Blank

Revision Source

<p>With tabbed browsing, there’s a reasonable chance that any given web page is in the background and thus not visible to the user. Some sites might want to behave differently in such a case. A few examples:</p>
<ul> <li>A site has an image carousel that shouldn’t advance to the next slide unless the user is viewing the page</li> <li>An application showing a dashboard of information doesn’t want to poll the server for updates when the page isn’t visible</li> <li>A page wants to detect when it is being prerendered so it can keep accurate count of pageviews</li>
</ul>
<p>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.</p>
<h4>document.webkitHidden</h4>
<p>Returns true if the page is in a state considered to be hidden to the user, and false otherwise.</p>
<h4>document.webkitVisibilityState</h4>
<p>Returns a string denoting the visibility state of the document. Possible Values:</p>
<ul> <li>“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. </li> <li>“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.</li> <li>“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.</li>
</ul>
<div class="note">
<p>//startSimulation and pauseSimulation defined elsewhere</p>
<div style="background-color: transparent; margin-top: 0px; margin-left: 0px; margin-bottom: 0px; margin-right: 0px; color: rgb(0, 0, 0); line-height: normal; "><span style="font-size: 11pt; font-family: 'Courier New'; color: rgb(0, 0, 0); background-color: transparent; font-weight: normal; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; ">function handleVisibilityChange() {</span><br>
<span style="font-size: 11pt; font-family: 'Courier New'; color: rgb(0, 0, 0); background-color: transparent; font-weight: normal; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> if (document.webkitHidden) {</span><br>
<span style="font-size: 11pt; font-family: 'Courier New'; color: rgb(0, 0, 0); background-color: transparent; font-weight: normal; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> pauseSimulation();</span><br>
<span style="font-size: 11pt; font-family: 'Courier New'; color: rgb(0, 0, 0); background-color: transparent; font-weight: normal; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> } else {</span><br>
<span style="font-size: 11pt; font-family: 'Courier New'; color: rgb(0, 0, 0); background-color: transparent; font-weight: normal; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> startSimulation();</span><br>
<span style="font-size: 11pt; font-family: 'Courier New'; color: rgb(0, 0, 0); background-color: transparent; font-weight: normal; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> }</span><br>
<span style="font-size: 11pt; font-family: 'Courier New'; color: rgb(0, 0, 0); background-color: transparent; font-weight: normal; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; ">}</span></div>
<div style="background-color: transparent; margin-top: 0px; margin-left: 0px; margin-bottom: 0px; margin-right: 0px; color: rgb(0, 0, 0); line-height: normal; ">document.addEventListener("webkitvisibilitychange", handleVisibilityChange, false);</div>
&lt;meta charset="utf-8"/&gt;</div>
<h3 name="Browser_Compatibility">Browser compatibility</h3>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
<table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Chrome**</th> <th>Firefox (Gecko)*</th> <th>Internet Explorer</th> <th>Opera*</th> <th>Safari (WebKit)</th> </tr> <tr> <td>Basic support</td> <td> <p>13</p> </td> <td> <p>{{ CompatNo() }}</p> <p> </p> </td> <td>{{ CompatNo() }}</td> <td>{{ CompatNo() }}</td> <td>{{ CompatNo() }}</td> </tr> </tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Android</th> <th>Firefox Mobile (Gecko)</th> <th>IE Phone</th> <th>Opera Mobile</th> <th>Safari Mobile</th> </tr> <tr> <td>Basic support</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> </tr> </tbody>
</table>
</div>
<h3 name="Specification">Specification</h3>
<p>Described in the <a class="external" href="http://www.w3.org/TR/page-visibility/">W3C Working Draft: Page Visibility</a></p>
<h3 name="See_also">See also</h3>
<p><a class=" external" href="http://www.html5rocks.com/en/mobile/workingoffthegrid.html">Blank</a></p>
Revert to this revision