You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何检测用户通过浏览器返回按钮访问网页的行为

如何检测用户通过浏览器返回按钮回到example.com?

假设访客访问了我的网站example.com,之后在同一标签页中访问了其他网站(可能是点击我网页的链接,也可能是手动在地址栏输入),随后点击浏览器返回按钮回到example.com。我该如何检测用户是通过返回按钮再次访问我的网站?

我尝试过的方法

使用Popstate:

window.addEventListener('popstate', function(event) {
  if (event.state && event.state.source === 'example.com') {
    console.log('User returned to example.com by clicking the back button');
  }
});

使用onpopstate:

window.onpopstate = function(event) {
  if (event && event.state) {
    console.log('User returned to example.com by clicking the back button');
  }
};

使用pageshow:

window.addEventListener('pageshow', function(event) {
  if (event.persisted) {
    console.log('User returned to example.com by clicking the back button');
  }
});

遗憾的是,这些方法都无效!

内容的提问来源于stack exchange,提问作者Mehul Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 20:32:05