有没有办法用 Javascript 跟踪选项卡上的焦点?
- 我们需要跟踪用户在网站上的有效时间
- 大多数用户在完成后都会将选项卡保持打开状态并移至另一个选项卡
- 网站上的时间非常不准确
是否有 Javascript 事件来跟踪用户的“失去焦点”当前选项卡?
- We need to track the EFFECTIVE time on site of our users
- Most users, when they're done, leave the tab open and move to another tab
- Time on site it's extremely inaccurate
Is there a Javascript Event to track the "loss of focus" of the current tab ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会使用
mousemove
和scroll
执行某些操作,只要在某个时间间隔内触发其中任何一个,就将访问者视为活动访问者。这也将涵盖他们保持浏览器打开和离开计算机的情况。I would do something with
mousemove
andscroll
and count a visitor as active as long as either of those trigger within some interval. That will also cover them leaving the browser open and leaving the computer.你说的是哪个标签?它是您的导航/菜单选项卡还是浏览器选项卡。我觉得,你指的是浏览器选项卡!
我认为准确地说是不可能的。但是,如果您跟踪一些事件(例如鼠标移动、焦点等),然后触发与服务器上的某些数据(计数器)相同的事件,该怎么办?当用户在您的页面上时,他会执行诸如移动鼠标、单击某处等操作。因此,首页加载和最后一个事件的差异可以告诉使用情况统计信息。
Which tab you are talking about? Is it your Nav/menu tab or Browser tab. I feel, you mean browser tab!
I think it is not possible accurately. But what if you track few events like mousemove, focus etc and then fire an event that same some data (counter) on server. When user is on your page then he will do something something like move mouse, click somewhere etc. So difference in first page load and last event can tell the usage stat.
虽然这个问题很早就提出了,但仍然可能会被人发现。在这种情况下,请使用页面可见性 API:
https://developer.mozilla .org/en-US/docs/Web/API/Page_Visibility_API
document.visibilityState
- 获取当前选项卡状态。document.onvisibilitychange
- 添加状态更改的回调。Though question was asked long ago, it might still be found by someone. In this case, use Page Visibility API:
https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API
document.visibilityState
- to get a current tab state.document.onvisibilitychange
- to add a callback for state change.这应该适用于选项卡切换和浏览器窗口失去焦点:
This should work both on tab switch and on browser window losing focus: