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

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

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>

Use ajax on events. use (async javascript request)

<script type="text/javascript"> 
       $(window).bind('unload', function(){
         $.ajax({
           type: "get",
           async: false,
           url: "URL CONTROLLER/FUNCTION"
         });               
    });
</script>