如何检测浏览器刷新或关闭? [英] How to detect browser Refresh or Close?

查看:64
本文介绍了如何检测浏览器刷新或关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我希望用户进行单点登录.当他离开应用程序时.顾名思义,它应该只在用户关闭当前浏览器选项卡或浏览器窗口或浏览器引用按钮时发生.它可以使用 JavaScript OnUnload 事件.但问题是这个事件发生在

In my application I want user to take an single signin. when he is leaving the application. As name explains, It should happen only when user closes current browser tab or browser window or browser referesh button. Its possible using JavaScript OnUnload event. But the problem is that this event occurs on

  1. 关闭浏览器标签
  2. 关闭浏览器窗口
  3. 点击任何内部页面链接(即锚点、表单按钮和按钮事件)
  4. 点击浏览器的刷新按钮
  5. 点击浏览器的后退/前进按钮

我已经处理了前 3 种情况,但无法检测到浏览器刷新和后退/前进按钮点击.有没有人实现过这样的功能,请帮帮我??

I have handled first 3 cases but not able to detect browser Refresh and Back/Forward button click. Has anybody of you implemented such functionality, Please help me??

提前致谢!!!

推荐答案

了解页面实际重新加载的更好方法是用户大多数新版本浏览器支持的导航器对象.它使用 HTML 5 导航时间 API.

A better way to know that the page is actually reloaded is user the navigator object that is supported by most of the new version browser. It uses the HTML 5 navigation time API.

//check for navigation time API support
if (window.performance) {
  console.info("window.performance work's fine on this browser");
}
  if (performance.navigation.type == 1) {
    console.info( "This page is reloaded" );
  } else {
    console.info( "This page is not reloaded");
  }

来源:https://developer.mozilla.org/en-US/docs/Web/API/Navigation_timing_API

回答者:Rahul Jain

这篇关于如何检测浏览器刷新或关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆