Created
September 18, 2014 15:55
-
-
Save pbojinov/d1596373498ff91bac2f to your computer and use it in GitHub Desktop.
Page Visibility API - http://code.kojilabs.com/page-visibility-api/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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