Skip to content

Instantly share code, notes, and snippets.

@paulirish
Created October 18, 2012 08:33
Show Gist options
  • Save paulirish/3910471 to your computer and use it in GitHub Desktop.
Save paulirish/3910471 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(){
var prefixes = ['webkit','moz','ms','o'];
if ('hidden' in document) return 'hidden';
for (var i = 0; i < prefixes.length; i++){
if ((prefixes[i] + 'Hidden') in document)
return prefixes[i] + 'Hidden';
}
}
function isHidden(){
var prop = getHiddenProp();
if (!prop) return false;
return document[prop];
}
// events
var evtname = getHiddenProp().replace(/[H|h]idden/,'') + 'visibilitychange';
document.addEventListener(evtname, fn)
// just logging it out. log it out, man.
console.log('getHiddenProp()', getHiddenProp());
console.log('isHidden()', isHidden());
function fn(){}
@felipemorais
Copy link

function getHiddenProp(){
var prefixes = ['webkitHidden','mozHidden','msHidden','oHidden', 'hidden'],l=prefixes.length;
while(l-- && !(prefixes[l] in document));
return prefixes[l];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment