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

John Doe
  • 983
  • 4
  • 14
  • 27
  • No and no - these events are outside the web page's control. – Pekka Dec 21 '12 at 11:58
  • 1
    Well as the guys below pointed out, the window.onblur event works quite fine for my requirements. – John Doe Dec 23 '12 at 23:21
  • 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! – Pekka Dec 24 '12 at 10:02
  • 1
    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 here – John Doe Dec 25 '12 at 23:25

3 Answers3

2

Trap the window.onblur event.

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

Alnitak
  • 334,560
  • 70
  • 407
  • 495
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.

Ruben Stolk
  • 12,386
  • 2
  • 18
  • 12
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

fabspro
  • 1,639
  • 19
  • 29