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



On Nov 3, 2: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.

According to the MSDN documentation about 'DHTML Events' (http://
msdn.microsoft.com/en-us/library/ms533051(VS.85).aspx), the
'onkeydown' event is the way to go for IE (as of IE 5), because:

a) As of IE 5,
* 'onkeydown' event can be cancelled for the BACKSPACE key by
specifying event.returnValue = false;
* 'onkeyup' event cannot be cancelled, although it fires for the
BACKSPACE key;

b) As of IE 4,
* 'onkeypress' event fires and can be canceled for the alphanumeric
keyboard keys (Letters: A - Z (uppercase and lowercase); Numerals: 0 -
9;
Symbols: ! @ # $ % ^ & * ( ) _ - + = < [ ] { } , . / ? \ | ' ` " ~;
and some System keys (ESC, SPACEBAR, ENTER). Notice that BACKSPACE is
not listed.


In addition, I've carried out some tests with the following results:
a) document.onkeyup doesn't work (obstruct 'Backspace key') for IE8,
FF3, Safari 3 and Opera 9.64;
b) document.onkeypress doesn't work for Safari 3 and IE 8, but it
works for FF3 and Opera 9.64;
c) document.onkeydown doesn't work for FF3 and Opera 9.64, but it
works for Safari 3 and IE 8.

So it's possible to solve the problem by referencing the listener
(disableBackspace) in both document.onkeypress (FF3 and Opera 9.64)
and document.onkeydown (okay for Safari 3 and IE 8) as below:

<script type="text/javascript">

function disableBackspace (e) {
var evt = window.event || e,
keynum = evt.keyCode ? evt.keyCode : evt.which;
if (keynum === 8) {
evt.returnValue = false; // IE
evt.preventDefault(); // W3C standards compliant browsers
window.alert("Backspace pressed. keyCode = " +keynum);
}
}

document.onkeydown = disableBackspace;
document.onkeypress = disableBackspace;
</script>


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.

Yep! 'return false' seems to be the cross-browser solution, but it
requires the (Jurassic)
<body onkeydown="return disableBackspace(event);">

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

I totally agree with you!

Cheers,
JR
.



Relevant Pages

  • Re: VK_BACK will launch IE in WinCE 5.
    ... Backspace, generally, in IE would go Back. ... on my <insert keyboard type here> on my Windows CE 5.0 device, ... QFE mentioned that Backspace key closes IE. ... RegisterHotKey() and set up for that to happen, but I'd be surprised if it ...
    (microsoft.public.windowsce.platbuilder)
  • Re: Problem behavior when overriding DeletePrefCharAction
    ... >> needs to be done to replace the backspace action in 1.4.2? ... > Are you familiar with Sun's Java Bug Database? ... handle action maps, backspace key handling, JTextPanes, etc. that might ...
    (comp.lang.java.programmer)
  • Re: Installing GIMPshop and fighting with Synaptic
    ... pressing the BackSpace key. ... edited the menurc file and inserted BackSpace between the quotes. ... Next version of GIMPshop should hopefully have the BackSpace ...
    (Ubuntu)
  • Re: Deleting rows or columns
    ... or column and pressing the Backspace key. ... Pressing Backspace deletes the rows or columns, ... Selecting a column and delting it was quite simple ...
    (microsoft.public.word.tables)