My intention is to prevent the Backspace key from navigating back to the previous URL in the history stack of browsers.
index.html
<html>
<head>
<script type="text/javascript" src="app.js"></script>
</head>
<body>
<input type="text">
</body>
</html>
app.js
window.addEventListener('keydown', function(e) {
if(e.key === 'Backspace') {
e.preventDefault();
}
}, false);
I used preventDefault() on key events with useCapture disabled. It did prevent the Backspace key from its default action. However, now I can't use Backspace key anywhere else (Eg: to delete text from the input box).
Why is it so? Aren't input boxes supposed to behave normally since window will get the event only at the very end (the event is registered in bubbling mode) and won't the preventDefault be called only then?