views:

938

answers:

4

On IE I can do this with the (terribly non-standard, but working) jQuery

if ($.browser.msie)
    $(document).keydown(function(e) { if (e.keyCode == 8) window.event.keyCode = 0;});

But is it possible to do in a way which works on Firefox, or in a cross-browser way for a bonus?

For the record:

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

does nothing.

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

solves the problem, but renders the backspace key unusable on the page, which is even worse than the original behaviour.

EDIT: The reason I do this is that I'm not creating a simple web page but a large application. It is incredibly annoying to lose 10 minutes of work just because you pressed backspace in the wrong place. The ratio of preventing mistakes vs. annoying users should be way above 1000/1 by preventing the backspace key from navigating back.

EDIT2: I'm not trying to prevent history navigation, just accidents.

+3  A: 

Not sure why no-one's just answered this - seems like a perfectly reasonable technical question to ask whether it's possible.

No, I don't think there's a cross-browser way to disable the backspace button. I know it's not enabled by default in FF these days though.

Brabster
+6  A: 

Here is a way to do it in Firefox and IE. However I would highly insist that you don't do this. As the comments state it's generally not a good idea to override default behavior unless it makes sense.

Lets say for example that you wanted to save a copy of this webpage for viewing offline, you pressed Ctrl+S and instead of your browser saving the web page, your browser just closes. How would that make you feel? Probably a little pissed.

Lucas McCoy
I kinda like that Google docs overrides the save function. But otherwise I agree.erikkallen: Are trying to prevent back in history altogether? You could loop through all inputs and textareas in the document in your function and only preventDefault if nothing has focus.
Jesse Kochis
That's not a fair comparison. The backspace key behavior is bad and hostile, and deserves to be disabled. ctrl+s is an entirely different kettle of fish.
Breton
@Breton: It doesn't matter if you think it's bad or not. The fact of the matter is that it behaves that way, and users expect it to behave that way. If it all the sudden doesn't, they'll complain, and with good reason.
Lucas McCoy
How did you style Ctrl and S to look like keyboard buttons?
Baddie
Using the <kbd> tag that Markdown provides. Take a look at the complete reference: stackoverflow.com/editing-help
Lucas McCoy
No user I have ever met expects it to behave that way. When my users discovered that it works that way, I get the blame by default. It was a mistake, and it should be undone. We should not enshrine default behavior as "the correct way" just because it's default. I've also had to disable the new draggable apis that browsers have now, because they ruin traditional mousedown/mouseup/mousemove drag and drop behavior. Default behaviors aren't always expected, and they aren't always a good idea. This is a very good case. It's a downright evil feature by any measure.
Breton
"y. If it all the sudden doesn't, they'll complain, and with good reason. " My users complain that it DOES behave this way. I don't think I'll get any complaints if I put this poor mutated puppy out of its misery.
Breton
@Breton: It doesn't matter which one of us is right. It is the standard, **not the default**. Every web browser that I know of uses the backspace key to navigate backwards.
Lucas McCoy
Which standard is it in? w3c? iso? rfc? do you have a link, a number, or a patent id? Do you have the results of the usability studies conducted about it?
Breton
And if it is a standard, so what? what's your point? Therefore it's good? Is this some new kind of informal fallacy?
Breton
@Lucas: I just pressed backspace and my browser did *not* navigate back (and no, this comment field did not have the focus) so not **Every** web browser does this (FF3 on Linux). Alt + Left-arrow OTOH...
Stephen P
+2  A: 

Based on the comments it appears you want to stop people losing information in forms, if they press backspace to delete but the field is not focused.

In which case, you want to look at the onunload event handler. Stack Overflow uses it - if you try to leave a page when you've started writing an answer, it pops up a warning.

DisgruntledGoat
That's a stupid solution. Got anything better?
Breton
sorry that was a rude comment. What I mean is, the user probably doesn't want to be berated for doing something they didn't even know was wrong. Why not just silently eat the key with an onkeydown? The goal is not to completely prevent the user from leaving the page, but to guard against this common mistake. In addition, pop up dialogues are not very effective, or useful. Users don't read them. Are you sure you want to leave the page? Okay!No wait wait, I didn't want to leave the page.. oops too late.
Breton
OK then take it or leave it. I think you're trying to over-engineer a problem that doesn't really exist, or at least isn't important. And in my experience, it's only power users who click "OK" on unfamiliar dialogs without reading them properly. ;)
DisgruntledGoat
@Disgruntled, You couldn't be more wrong. If you don't believe me, you could believe Raymond: http://blogs.msdn.com/oldnewthing/archive/2003/09/01/54734.aspx
erikkallen
Are you and Breton the same person? Anyway you kinda shot yourself in the foot there - the article says "the default answer is Cancel" so when seeing the dialog about leaving the page, they're going to press "Cancel" and therefore **not** leave the page. Though it should be noted that Chrome's options on that dialog are "Leave this page" and "Stay on this page" which are very clear.
DisgruntledGoat
No, he is not me, but I've come across that link before. I think the comments are hilarious. "What!? people ignore modal dialog boxes? We must find a way to make them even MORE persistent and annoying!". Really explains a lot about microsoft software.
Breton
@Disgruntled: No, I'm not Breton, and the point of the article is not "The default choice is..." but "users don't read anything."
erikkallen
A: 

This code solves the problem, at least in IE and Firefox (haven't tested any other, but I give it a reasonable chance of working if the problem even exists in other browsers).

// Prevent the backspace key from navigating back.
$(document).keydown(function(e) {
    var doPrevent;
    if (e.keyCode == 8) {
        var d = e.srcElement || e.target;
        if (d.tagName.toUpperCase() == 'INPUT' || d.tagName.toUpperCase() == 'TEXTAREA') {
            doPrevent = d.readOnly || d.disabled;
        }
        else
            doPrevent = true;
    }
    else
        doPrevent = false;

    if (doPrevent)
        e.preventDefault();
});
erikkallen