Skip to content

Instantly share code, notes, and snippets.

@mediaupstream
Last active March 22, 2018 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mediaupstream/649ec24b001189973c9de83887e54b8a to your computer and use it in GitHub Desktop.
Save mediaupstream/649ec24b001189973c9de83887e54b8a to your computer and use it in GitHub Desktop.
Check if the current page is hidden or active
function checkPageVisibility() {
let eventName = 'visibilitychange'
let hiddenProp = 'hidden'
// set proper event name for other browsers
if (typeof document.msHidden !== 'undefined') {
hiddenProp = 'msHidden'
eventName = 'msvisibilitychange'
} else if (typeof document.webkitHidden !== "undefined") {
hiddenProp = 'webkitHidden'
eventName = 'webkitvisibilitychange'
}
document.addEventListener(eventName, () => {
//
// When the visibility of your page changes, do something
//
console.log('Is the page hidden?', document[hiddenProp])
}, false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment