My current web application gets the key press of the user, however, if the user hits the delete or backspace button it sends the user back a page. Is their a way of preventing this?
Asked
Active
Viewed 247 times
0
-
Why your application sendes th user a page back if the user hits the delete or backspace? – idmean Mar 16 '13 at 15:09
-
no it doesn't send them back. I think it is a default setting on browsers for if some element isn't focused and they hit the back button it will automatically send them back a page. – npage Mar 16 '13 at 15:11
-
1@juhana Yes it does, I know older version of IE definitely do this. Current versions of Chrome do as well. – JaredMcAteer Mar 16 '13 at 15:12
-
even the latest version of google chrome does it. – npage Mar 16 '13 at 15:13
1 Answers
2
jQuery makes this easy. You can easily replace this with vanilla JS and use addEventListener()
for class A browsers and attachEvent()
for legacy IE, and normalize keyCode
and charCode
yourself. But why?
$(document).on('keyup', function(e){
var keycode = e.keyCode ? e.keyCode : e.which;
if(keycode == 46){ // delete
e.stopPropagation();
e.preventDefault();
}
});
The backspace key keyCode 8
is detectable from within a focused input only and will likely still emit your undesired behavior, but it'll keep the user happy.
Here is a complete list of keyCodes

AlienWebguy
- 76,997
- 17
- 122
- 145