Call off Backspace action

Asked

Viewed 63 times

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 go back. I separated the function of the trigger from pressing the Backspace key because it is used elsewhere in the code. How can I cancel Backspace action if the user does not confirm?

  • If you want to avoid browsing out of the page I think you have to cancel on the keydown, otherwise the keyup is too late.

  • @Sergio I changed to keydown. Regardless of that, I still keep the same problem. Despite the 'Return false', it performs the Backspace action

  • You have to give return... forehead: return ConfirmarVolta();

1 answer

3


I think you need to use the keydown so you can stop the event and you have to use return the value of the function to give return false; if the function gives false in return.

Forehead like this:

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

example: http://jsbin.com/femacowuka/2/

  • It worked, but I took the code straight from jsbin and in case, it was as 'keydown' and not 'keyup' as in the answer from here.

  • 1

    @Rafaelbarbosa because I explained right but I put wrong here, already corrected. Thank you. Good that it is working.

Browser other questions tagged

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