views:

316

answers:

3

I use the following code to delete a character when the user presses the delete code. It works in Firefox but doesn't work in Google Chrome. What do I have to fix?

  window.onkeypress = function(key) {
        ...
        if (Guessing>0){
            else if (key.keyCode == 8){//Delete key
                PhraseEntry=PhraseEntry.substring(0, PhraseEntry.length - (1+back))
                    + PhraseEntry.substring(PhraseEntry.length - back, PhraseEntry.length );
                $("display_text").empty( ).append(PhraseEntry);
            }
        }
    }
    ...
+1  A: 
<script type="text/javascript">
    window.onkeypress = function(key)
        {
            alert(key.keyCode);
        }
</script>

I'd start with this - put that in your page and hit Delete when viewing it in Chrome :)

inkedmn
+1  A: 

Handle the keydown event instead.

Tim Down
+2  A: 

Try the keydown event instead (Delete is keyCode 46):

window.onkeydown = function(e) {
    if (e.keyCode == 46) { // Delete key
        // ...
    }
}
Crescent Fresh