Last active
March 22, 2018 16:54
-
-
Save mediaupstream/649ec24b001189973c9de83887e54b8a to your computer and use it in GitHub Desktop.
Check if the current page is hidden or active
This file contains hidden or 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
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