Prevent the backspace key from going to the previous page

.keydown(function(e){
        alert(e.keyCode);
...

      

This returns 8 as the backspace key. The problem is the browser is being sent back to history!

This is the most annoying behavior of Windows browsers, I am just trying to clear the values ​​of the list fields using the delete and backspace keys. I hate this behavior and it is exactly the same in IE and Chrome.

Any ideas I could use for the backspace key?

+3


source to share


2 answers


Just use preventDefault()

:

$(document).keydown(function(e){
    if ( e.keyCode == 8 ) e.preventDefault();
});

      



Note that this will prevent backspaces from working on input fields. This can be easily fixed as follows:

$(document).keydown(function(e){
    if ( e.keyCode == 8 && e.target.tagName != 'INPUT' && e.target.tagName != 'TEXTAREA') {
        e.preventDefault();
    }
});

      

+9


source


Use the preventDefault () function, and as an additional protection, you can use the onbeforeunload event to warn people if they move away from your page.



0


source







All Articles