Detect change of browser tabs with javascript

2019-04-29 13:21发布

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

3条回答
ゆ 、 Hurt°
2楼-- · 2019-04-29 13:43

Trap the window.onblur event.

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

查看更多
聊天终结者
3楼-- · 2019-04-29 13:46

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.

查看更多
Ridiculous、
4楼-- · 2019-04-29 14:01

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

查看更多
登录 后发表回答