Detect change of browser tabs with javascript
Asked Answered
C

3

6

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

Creosote answered 21/12, 2012 at 11:56 Comment(4)
No and no - these events are outside the web page's control.Improvisation
Well as the guys below pointed out, the window.onblur event works quite fine for my requirements.Creosote
Okay - although that just tells you that the current window lost focus, not who it lost focus to. Anyway, if Alnitak's answer helped you, consider marking it accepted using the check mark!Improvisation
It does not matter really who it lost focus to, a website does not need to know everything. And thx for the hint with the check mark, I am new hereCreosote
D
3

Trap the window.onblur event.

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

Dutiable answered 21/12, 2012 at 11:58 Comment(0)
B
1

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.

Branchiopod answered 21/12, 2012 at 11:58 Comment(0)
F
1

The Page Visibility API was designed to do this. Have a read at this URL:

https://developer.mozilla.org/en-US/docs/Web/Guide/User_experience/Using_the_Page_Visibility_API

Fillagree answered 26/3, 2014 at 2:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.