Created
November 14, 2015 10:17
-
-
Save vingkan/1db43dceb2cc1b5dcc70 to your computer and use it in GitHub Desktop.
Show two simple approaches to determining if the user is on a given tab/window.
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
/* | |
* Using HTML5 Visibility State | |
* Triggers when user goes to a different tab | |
* Triggers when user minimizes browser | |
* Does not trigger when user opens another window on top of browswer | |
*/ | |
document.visibilityState; | |
setInterval(function(){ | |
console.log(document.visibilityState); | |
}, 500); | |
/* | |
* Using JQuery Events | |
* Triggers when user goes to a different tab | |
* Triggers when user minimizes browser | |
* Triggers when user opens another window on top of browswer | |
*/ | |
$(window).focus(function(){ | |
console.log('focus'); | |
}); | |
$(window).blur(function(){ | |
console.log('blur'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment