0

I'd like to check if the user is currently viewing the browser tab, like if you are at google chrome and you have facebook, twitter and the application, and you want to know if the application is selected between all 3. Is there a way to do it with purely with angular or do I need to do something like this:

$(window).focus(function() {
  console.log('enter');
}).blur(function() {
  console.log('out');
});
Matheus Lima
  • 2,103
  • 3
  • 31
  • 49
  • what do you mean by "viewing the tab"? You can check which tab is selected thanks to your controller http://jsfiddle.net/j0wu52ar/ – Frederic Le Feurmou Nov 24 '14 at 18:35
  • Looks like a duplicate of http://stackoverflow.com/questions/3479734/javascript-jquery-test-if-window-has-focus . AngularJS has no special tools for this. – hon2a Nov 24 '14 at 18:40
  • is not like this. is the tab in the browser, like if you have facebook, twitter and youtube opened, and you want to know if you are inside facebook tab – Matheus Lima Nov 24 '14 at 18:59

1 Answers1

3

AngularJS comes bundled with JQLite (subset of jQuery). You can inject the mock $window to keep it testable or just use window. Your call :

angular.element($window).bind('focus', function() {
  console.log('enter');
}).bind('blur', function() {
  console.log('out');
});
AlienWebguy
  • 76,997
  • 17
  • 122
  • 145