Sakkeer A
Sakkeer A

Reputation: 1100

How to Find the Browser Tab is Focused or Not using HostListener in Angular 2+

I want the Browser Tab Focus and Blur Event using HostListener.

Upvotes: 4

Views: 6993

Answers (1)

Sakkeer A
Sakkeer A

Reputation: 1100

First Import HostListener in your Component

import { HostListener } from '@angular/core';

Then put this code to get Tab Focus and Blur Event

export class AppComponent { 

   @HostListener('window:focus', ['$event'])
   onFocus(event: FocusEvent): void {

       // Do something      

   }

   @HostListener('window:blur', ['$event'])
   onBlur(event: FocusEvent): void {

      // Do something

   }

  ........
  .....
}

Upvotes: 10

Related Questions