Skip to content

Instantly share code, notes, and snippets.

@carlcalderon
Forked from paulirish/gist:3910471
Created October 18, 2012 15:28
Show Gist options
  • Save carlcalderon/3912567 to your computer and use it in GitHub Desktop.
Save carlcalderon/3912567 to your computer and use it in GitHub Desktop.
page visibility API : tribulations with prefixes
// this is the least sucky way i could think of to
// detect and deal with a cross-browser impl of the page visibility api
// forks welcome.
function getHiddenProp() {
if ('hidden' in document) return 'hidden';
var prefixes = ['webkit','moz','ms','o'],
len = prefixes.length,
i = 0;
for ( ; i < len; i++) {
if ((prefixes[i] + 'Hidden') in document)
return prefixes[i] + 'Hidden';
}
}
function isHidden() {
return document[getHiddenProp()] || false;
}
// events
var evtname = getHiddenProp().replace(/hidden/i, 'visibilitychange');
document.addEventListener(evtname, fn)
// just logging it out. log it out, man.
console.log('getHiddenProp()', getHiddenProp());
console.log('isHidden()', isHidden());
function fn(){}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment