Prevent the backspace key from going to the previous page



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?


source to share

2 answers

Just use preventDefault()


    if ( e.keyCode == 8 ) e.preventDefault();


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

    if ( e.keyCode == 8 && != 'INPUT' && != 'TEXTAREA') {




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.



All Articles