Capture event close window

0

I need to perform an action when the user closes a window on my PHP system. I found a JS code that does this in IE, but in Firefox it does not work:

    document.onkeydown = fecharIE;
    window.onbeforeunload = fecharIE;
    //for IE
    function fecharIE (evt){
         var iX = window.document.body.offsetWidth - window.event.clientX ;
         var iY = window.event.clientY ;
        if (iX <=30 && iY < 0){
           // quando botão de fechar da janela é acionado
           sair();
        }

        if (!evt)
            evt = event;
        if (event.altKey && event.keyCode==115){
         //ALT+F4
          sair();
        }
    }

Does anyone have a solution for Firefox?

    
asked by anonymous 18.01.2016 / 19:16

2 answers

1

The example below works well for current browsers:

<html>
  <head>
    <script type="text/javascript">
      window.onbeforeunload = function() {
          return "Tem certeza que quer fechar?"
      }
    </script>
  </head>
  <body>
  </body>
</html>
    
18.01.2016 / 20:04
0

Good morning! Another way to do this is by doing:

<script>
  var script = function() {
    //Faço qualquer coisa que eu precise
    return 'Retorno que aparece na tela';
  }
  window.addEventListener("beforeunload", script);
</script>

This "// Return that appears on the screen" appears when you click to exit the page and it shows a message (String being returned) and asks "Exit the page" or "Continue on page". >     

19.01.2016 / 15:21