Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 827
Sakkeer A Avatar asked Sep 05 '25 13:09

Sakkeer A


1 Answers

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

   }

  ........
  .....
}
like image 78
Sakkeer A Avatar answered Sep 08 '25 04:09

Sakkeer A