Capture event close window

Asked

Viewed 1,071 times

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 Firefox 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();
        }
    }

Someone has the solution for Firefox?

  • Here we use firefox and always works with onbeforeunload and onunload.

  • In the same way that it is in the code posted?

  • In fact we use direct window.onunload = function(e){

2 answers

1


The example below works well in current browsers:

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

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 that is being returned) and asks "Exit page" or "Continue to page".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.