Skip to content

Instantly share code, notes, and snippets.

@vingkan
Created November 14, 2015 10:17
Show Gist options
  • Save vingkan/1db43dceb2cc1b5dcc70 to your computer and use it in GitHub Desktop.
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.
/*
* 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