Cancel backspace action

2

I made the following script

 $(document).ready(function () {
            $('html').keydown(function (e) {
                if (e.keyCode == 8)
                {
                    ConfirmarVolta();
                }
            });
        });

  function ConfirmarVolta() {
        if (confirm("Deseja voltar a tela inicial de cadastro de pedido? O progresso no pedido atual será perdido.")) {
            location.href = "pedidoInserir.aspx";
        } else {
            return false;
        }
    }

And I hoped that by clicking 'cancel' on the popup, the screen would not suffer go back. I separated the trigger function from pressing the backspace key because it is used in another part of the code. How can I cancel the backspace action if the user does not confirm?

    
asked by anonymous 11.05.2015 / 22:14

1 answer

3

I think you have to use keydown to stop the event and you have to use return of the function value to thereby give return false; if the function gives false as a return.

Test like this:

 $(document).ready(function () {
     $('html').keydown(function (e) {
         if (e.keyCode == 8) {
             return ConfirmarVolta();
         }
     });
 });

example: link

    
11.05.2015 / 22:22