Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since originally writing this answer, a new specification has reached <em>recommendation</em> status thanks to the W3C. The <a href="http://www.w3.org/TR/page-visibility/" rel="noreferrer">Page Visibility API</a> (on <a href="https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API" rel="noreferrer">MDN</a>) now allows us to more accurately detect when a page is hidden to the user.</p> <p>Current browser support:</p> <ul> <li>Chrome 13+</li> <li>Internet Explorer 10+</li> <li>Firefox 10+</li> <li>Opera 12.10+ <sup>[<a href="https://dev.opera.com/blog/page-visibility-api-support-in-opera-12-10/" rel="noreferrer">read notes</a>]</sup></li> </ul> <p>The following code makes use of the API, falling back to the less reliable blur/focus method in incompatible browsers.</p> <pre><code>(function() { var hidden = "hidden"; // Standards: if (hidden in document) document.addEventListener("visibilitychange", onchange); else if ((hidden = "mozHidden") in document) document.addEventListener("mozvisibilitychange", onchange); else if ((hidden = "webkitHidden") in document) document.addEventListener("webkitvisibilitychange", onchange); else if ((hidden = "msHidden") in document) document.addEventListener("msvisibilitychange", onchange); // IE 9 and lower: else if ("onfocusin" in document) document.onfocusin = document.onfocusout = onchange; // All others: else window.onpageshow = window.onpagehide = window.onfocus = window.onblur = onchange; function onchange (evt) { var v = "visible", h = "hidden", evtMap = { focus:v, focusin:v, pageshow:v, blur:h, focusout:h, pagehide:h }; evt = evt || window.event; if (evt.type in evtMap) document.body.className = evtMap[evt.type]; else document.body.className = this[hidden] ? "hidden" : "visible"; } // set the initial state (but only if browser supports the Page Visibility API) if( document[hidden] !== undefined ) onchange({type: document[hidden] ? "blur" : "focus"}); })(); </code></pre> <p><code>onfocusin</code> and <code>onfocusout</code> are <a href="http://www.thefutureoftheweb.com/blog/detect-browser-window-focus" rel="noreferrer">required for IE 9 and lower</a>, while all others make use of <code>onfocus</code> and <code>onblur</code>, except for iOS, which uses <code>onpageshow</code> and <code>onpagehide</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload