Skip to content

Instantly share code, notes, and snippets.

@pbojinov
Created September 18, 2014 15:55
Show Gist options
  • Save pbojinov/d1596373498ff91bac2f to your computer and use it in GitHub Desktop.
Save pbojinov/d1596373498ff91bac2f to your computer and use it in GitHub Desktop.
/**
* The Page Visibility API adds two new properties to the document object.
*
* document.hidden
* document.visibilityState
*
* The hidden property is a boolean representing the current visibility of the page.
*
* The visibilityState property is a string that gives more information about the current page state. This property has four possible values:
*
* hidden – not visible on any screen
* visible – visible to the user
* prerender – the page loaded off-screen and is not visible
* unloaded – the user is navigating away from the page
*/
function getPrefixed() {
// Check for support of the un-prefixed property.
if ('hidden' in document) {
// No prefix needed, return null.
return 'hidden';
}
// Create an array of the possible prefixes.
var prefixes = ['moz', 'ms', 'o', 'webkit'];
// Loop through each prefix to see if it is supported.
for (var i = 0; i < prefixes.length; i++) {
var testPrefix = prefixes[i] + 'Hidden';
if (testPrefix in document) {
// Prefix is supported!
// Return the current prefixed property name.
return testPrefix;
}
}
// The API must not be supported in this browser, return null.
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment