Re: event handler in <body> doesn't work



On Nov 3, 5:45 am, David Mark <dmark.cins...@xxxxxxxxx> wrote:
On Nov 2, 8:04 pm, JR <groups_j...@xxxxxxxxxxxx> wrote:

[snip]

It's really hard to say, but IE 8 (maybe 7 too?) works just fine with
document.onkeydown and event.returnValue = false, whereas FF3 and
others work well with document.onkeypress and e.preventDefault().

First off, keydown doesn't enter into this.  As backspace is a
printable character, keypress is where you would expect to deal with
it.  IIRC, this key (8) is an exception in IE (meaning you have to
deal with it in keyup).



So,
I've written code specifically for both cases, which can be put either
in a external .js file or at the bottom of the body tag (just before </
body>):

<script type="text/javascript">
  document.onkeydown = function () {  // IE
    var e = window.event, keynum = e.keyCode;
    if (keynum === 8) {
      e.returnValue = false;
      window.alert("Backspace pressed. keyCode = " +keynum);
    }
  };

  document.onkeypress =  function (evt) { // FF3, Safari, etc.
    var keynum = evt.which;
    if (keynum === 8) {
      evt.preventDefault();  // Other W3C compliant browsers
      window.alert("Backspace pressed. keyCode = " +keynum);
    }
  };
</script>

Start again.  Use one listener function, attach to keypress and
keyup.  First time through you can tell which to ignore.  And if you
are going to use DOM0 (e.g. onkeypress), return false to prevent the
default action.

And ultimately, this is a horrible idea (breaking the back key).


as suggested, if I process the backspace-key for IE in keydown and for
FF in keypress it works well.

I don't want to break the standard, it's just that i try to understand

thank you all for your help!
Chris
.



Relevant Pages

  • Re: event handler in doesnt work
    ... keydown doesn't enter into this. ... deal with it in keyup). ... Use one listener function, attach to keypress and ...
    (comp.lang.javascript)
  • Re: Tastencodes/Events stören im Folge-Formular
    ... KeyDown, KeyUp und KeyPress z.B. im Ausgabefenster ... Das ist eher ein Problem in Deinem Programmdesign, ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • Re: keyboard events and characters
    ... about to be plunked into a textarea based on keyboard input (from ... keydown, keypress, keyup, or other events)? ...
    (comp.lang.javascript)
  • Re: KeyPress method
    ... Unless the key is going to repeat, use KeyUp. ... Windows key and hold it for a few seconds. ... > in KeyUp or KeyDown? ... produce KeyPress events. ...
    (microsoft.public.vb.general.discussion)
  • Re: Trap keystrokes/keyboard events in controls
    ... In KeyPress if no modifier found in keydown then just print the ... As Mike says, you can preview key input to deal with situations where some control is consuming the input, but you still want to see it. ...
    (microsoft.public.dotnet.languages.csharp)