Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect change of browser tabs with javascript

is there some way of detecting using javascript that a user has switched to a different tab in the same browser window.

Additionally is there a way to detect a user has switched to a different window than the browser?

thank you

like image 728
John Doe Avatar asked Dec 21 '12 11:12

John Doe


People also ask

Can websites detect switching tabs?

Modern websites use multiple "event listeners" and can detect every move the user is executing. So, if a user switches the tab or hovers over to another tab, it can gather the data and see whether the user stayed on the web page or not. A website can detect this anomaly by using cookies and IDs.

How does Tab work in JavaScript?

In the web page tab key works as an html element navigator, which means if you push the tab key button, it moves current element focus to the next element. This can be kind of hassle to the user of certain web based text editor, especially to the programmer.


2 Answers

Trap the window.onblur event.

It's raised whenever the current window (or tab) loses focus.

like image 111
Alnitak Avatar answered Sep 22 '22 12:09

Alnitak


Most probably there is no standards javascript for this. Some browsers might support it but normally there is only a window.onblur event to find out the user has gone away from the current window.

like image 21
Ruben Stolk Avatar answered Sep 24 '22 12:09

Ruben Stolk