Prevent BACKSPACE from navigating back with jQuery (Like Google's Homepage)

javascript, jquery

Solution

I would bind an event handler to `keydown` and prevent the default action of that event if we're dealing with the backspace key outside of a `textarea` or `input`:

$(document).on("keydown", function (e) {
    if (e.which === 8 && !$(e.target).is("input, textarea")) {
        e.preventDefault();
    }
});

Problem

Notice while on Google's homepage, with no focus on any element, pressing BACKSPACE will put the focus into the search toolbar instead of navigating back. How can I accomplish this? I keep running into this problem with users in my app. They don't have focus on any element and hit BACKSPACE which throws them out of the app.

Original source

Related problems