0

I am attempting to stop a backspace keydown event from being handled by browsers, I'm using the jQuery library, so I need to get the original event, but on some browsers (Firefox at least) I get an error when trying to set the original events keyCode = 0, it gives and error saying that only a getter exists for that property.

function blockBackspace(event) {
    var altKey = event.originalEvent.altKey;
    var srcElementType = event.originalEvent.srcElement;
    if( (altKey) || ((event.keyCode == 8)  && (srcElementType != "text" && srcElementType != "textarea" && srcElementType != "password"))
        || ((event.ctrlKey) && ((event.keyCode == 78) || (event.keyCode == 82)) ) || (event.keyCode == 116) )
        {
        event.keyCode = 0;
        event.returnValue = false;
        event.originalEvent.keyCode = 0;
        event.originalEvent.returnValue = false;
        //sets process backspaceFlag to keep multiple handlers from removing text
        processBackspace = true;
    }
}

So I'm not exactly sure what to do next, every solution I find yields more problems. There must be ways around this problem or else other text areas (that's kind of what I'm building) would not work.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
walnutmon
  • 5,873
  • 7
  • 42
  • 57
  • Why would you want to stop it? – Gumbo Mar 13 '10 at 19:14
  • 2
    @Gumbo - I've had this before, user is typing in a input box that's part of an ajax refresh, they hit it at a bad moment and leave the page. There is always another method, but I know I've pondered doing this a few times. – Nick Craver Mar 13 '10 at 19:16
  • I'm developing a page that allows user input into a custom component that doesn't use an input/text box, the input is similar to a command prompt, but any typing experience for a user stinks if they can't use backspace – walnutmon Mar 15 '10 at 12:35

1 Answers1

1

You can't stop the event from happening. One alternative is to use the beforeunload proprietary event that asks the user if they really want to exit the page.

$(window).bind('beforeunload', function() {
    return "You want to leave the best page in the universe?";
});​
Anurag
  • 140,337
  • 36
  • 221
  • 257
  • this doesn't work for me, I need to stop the browser from going back in order to allow them to type into a custom text input system if you check out the code I posted it actually works on some browsers... but firefox is giving me an access violation on the property that I'm trying to change – walnutmon Mar 15 '10 at 12:36