有没有办法用 Javascript 跟踪选项卡上的焦点?

发布于 2024-09-17 06:32:25 字数 142 浏览 20 评论 0原文

  • 我们需要跟踪用户在网站上的有效时间
  • 大多数用户在完成后都会将选项卡保持打开状态并移至另一个选项卡
  • 网站上的时间非常不准确

是否有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

可是我不能没有你 2024-09-24 06:32:26

我会使用 mousemovescroll 执行某些操作,只要在某个时间间隔内触发其中任何一个,就将访问者视为活动访问者。这也将涵盖他们保持浏览器打开和离开计算机的情况。

I would do something with mousemove and scroll 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.

━╋う一瞬間旳綻放 2024-09-24 06:32:26

你说的是哪个标签?它是您的导航/菜单选项卡还是浏览器选项卡。我觉得,你指的是浏览器选项卡!
我认为准确地说是不可能的。但是,如果您跟踪一些事件(例如鼠标移动、焦点等),然后触发与服务器上的某些数据(计数器)相同的事件,该怎么办?当用户在您的页面上时,他会执行诸如移动鼠标、单击某处等操作。因此,首页加载和最后一个事件的差异可以告诉使用情况统计信息。

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.

如梦亦如幻 2024-09-24 06:32:26

虽然这个问题很早就提出了,但仍然可能会被人发现。在这种情况下,请使用页面可见性 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.

平生欢 2024-09-24 06:32:26
  <script>
    document.onvisibilitychange = () => {
      if (document.visibilityState === "hidden") {
        console.log("tab inactive");
      }
      if (document.visibilityState === "visible") {
        console.log("tab active");
      }
    };
  </script>
  <script>
    document.onvisibilitychange = () => {
      if (document.visibilityState === "hidden") {
        console.log("tab inactive");
      }
      if (document.visibilityState === "visible") {
        console.log("tab active");
      }
    };
  </script>
暗地喜欢 2024-09-24 06:32:25

这应该适用于选项卡切换和浏览器窗口失去焦点:

function onBlur() {
    document.body.className = 'blurred';
};
function onFocus(){
    document.body.className = 'focused';
};

if (/*@cc_on!@*/false) { // check for Internet Explorer
    document.onfocusin = onFocus;
    document.onfocusout = onBlur;
} else {
    window.onfocus = onFocus;
    window.onblur = onBlur;
}

This should work both on tab switch and on browser window losing focus:

function onBlur() {
    document.body.className = 'blurred';
};
function onFocus(){
    document.body.className = 'focused';
};

if (/*@cc_on!@*/false) { // check for Internet Explorer
    document.onfocusin = onFocus;
    document.onfocusout = onBlur;
} else {
    window.onfocus = onFocus;
    window.onblur = onBlur;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文