Home » Jquery » javascript – Is there any way to detect window layover on a browser?

javascript – Is there any way to detect window layover on a browser?

Posted by: admin February 22, 2020 Leave a comment

Questions:

I am working on a web-app which should do some function when the user moves away from that tab.
I managed to detect tab switching, and also browser switching through

window.addEventListener('visibilitychange', () => this.dothis())

But still i can’t detect if a user opens system-settings like preferences or settings, or any other built-in system tool. Is there a way to detect those?

How to&Answer:

You can try if the blur event does what you need. It triggers when the active tab loses focus to another tab, another window or another application.

If needed, you can combine it with the focus event, which is triggered when the opposite happens (a non-focused tab gains focus).

window.addEventListener('blur', () => /* ... */ );

window.addEventListener('focus', () => /* ... */ );