dongzhi1851 2017-11-02 05:24
浏览 43

如何检测浏览器刷新或关闭? [关闭]

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. close of browser tab
  2. close of browser window
  3. click of any internal page link(i.e anchors, form buttons and buttons events)
  4. click of browser's Refresh button
  5. click of browser's Back/Forward button

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??

Thanks in advance!!!

  • 写回答

2条回答 默认 最新

  • duansengcha9114 2017-11-02 06:01
    关注

    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");
      }

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

    Answered By : Rahul Jain

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示