0

I want to capture browser close event using javascript. I googled but I am not getting any solution anywhere,below is my code where I have handled anchor, Form Submit and Submit button on onbeforeunload.

<script>
var validNavigation = false;
function wireUpEvents() {
   window.onbeforeunload = function() {
    if (!validNavigation) {
      // invalidate session
    }
  }

   // Attach the event keypress to exclude the F5 refresh
   $(document).bind('keydown', function(e) {
     if (e.keyCode == 116){
     validNavigation = true;
    }
  });

  // Attach the event click for all links in the page
  $("a").bind("click", function() {   
    validNavigation = true;
  });

  // Attach the event submit for all forms in the page
  $("form").bind("submit", function() {
     validNavigation = true;
  });

  // Attach the event click for all inputs in the page
  $("input[type=submit]").bind("click", function() {
     validNavigation = true;
  });

 }

 // Wire up the events as soon as the DOM tree is ready
 $(document).ready(function() {
  wireUpEvents();  
 });
</script>

Is there any way I can capture browser close button event, I have seen developers using X and Y axis but that is not recommended most of developers.

Thanks..

pise
  • 849
  • 6
  • 24
  • 51
  • You can't directly capture the *close button event*, since this happens outside the page (and thus is inaccessible to your script); the closest thing you can do is `onbeforeunload`, when the page is unloaded (right before it's either closed or reloaded). Why does that not work for you? (Also, what are you really trying to do?) – Frxstrem May 28 '15 at 10:15
  • @Frxstrem but what if some click on refresh button on browser then also session will get invalidate. – pise May 28 '15 at 10:16
  • If you're trying to differentiate a page being refreshed and a page being closed, I don't think that's possible. – Frxstrem May 28 '15 at 10:19
  • @Frxstrem no way to find using x and y co ordinates also, will that be wrong because since yesterday I am trying to find a solution. – pise May 28 '15 at 10:20
  • possible duplicate: http://stackoverflow.com/questions/20853142/trying-to-detect-browser-close-event – Avinash May 28 '15 at 11:11

1 Answers1

1

You can try this .. prompting user to confirm before closing tab. and you can do some thing you need

<script language="JavaScript">
      window.onbeforeunload = confirmExit;
      function confirmExit()
      {
        return "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?";
      }
    </script>
Zohaib Waqar
  • 1,204
  • 11
  • 18
  • 1
    this will be called even if browser refresh button is clicked. – pise May 28 '15 at 11:34
  • **window.onbeforeunload ** use to detect refresh and close ** window.onload ** used to detect refresh but there is not method to detect close only.. you have to do some trick.. if i find it will share it to you – Zohaib Waqar May 28 '15 at 12:24