Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Using the PageVisibility API

Using the PageVisibility API

This is a quick intro to the PageVisibility API which landed in most of the current web browsers and enables some really nice features.

Given at http://berlinjs.org at 21st March 2013.

Robin Mehner

March 21, 2013

More Decks by Robin Mehner

Other Decks in Technology


  1. Robin Mehner @rmehner - running a small “agency” doing web

    development - Berlin.JS, up.front, BEPHPUG, Reject.js, NodeCopter bla bla bla
  2. What’s the PageVisibility API? With all the HTML5 JS posterchilds

    like Canvas, WebWorkers, FileReader and all the other stuff that you can use if you start your browser with 25 flags, it’s one of the most overlooked additions to our toolbelt
  3. Gives you an API to react on visibility changes of

    the tab document.hidden // boolean document.visibilityState // ‘hidden’, ‘visible’ or ‘prerender’ ‘visibilitychange’ event
  4. stop animations stop video playback give the user a digest

    of the latest changes when he’s back stop polling pause/resume a game stop playing sounds (that annoying “which tab is playing sound?”) Use cases Not only for saving battery, but also for a lot of additional UX stuff
  5. Browser support is pretty good. There’s one (major) browser who

    does not support it in it’s current version ... Safari. http://caniuse.com/#feat=pagevisibility (Also the best site ever, since Amazing Horse.)
  6. Don’t be fooled by pagehide / pageshow Unless you know

    what you’re doing. Sound like the right deal, but they’re just onload / onunload with cache semantics. Different behaviour in iOS: * iPhone fires the event when the user switches the tab, iPad not * iOS stops execution of JS when you switch a tab * workaround with heartbeat