Re: event handler in <body> doesn't work
- From: JR <groups_jr-1@xxxxxxxxxxxx>
- Date: Tue, 3 Nov 2009 15:56:32 -0800 (PST)
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
.
- Follow-Ups:
- Re: event handler in <body> doesn't work
- From: David Mark
- Re: event handler in <body> doesn't work
- References:
- Re: event handler in <body> doesn't work
- From: JR
- Re: event handler in <body> doesn't work
- From: Chris
- Re: event handler in <body> doesn't work
- From: Chris
- Re: event handler in <body> doesn't work
- From: JR
- Re: event handler in <body> doesn't work
- From: Chris
- Re: event handler in <body> doesn't work
- From: JR
- Re: event handler in <body> doesn't work
- From: JR
- Re: event handler in <body> doesn't work
- From: David Mark
- Re: event handler in <body> doesn't work
- Prev by Date: Re: Intrusive DIV scrollbar needs Javascript or CSS to "append"?
- Next by Date: FAQ Topic - I have window.status="Moomin"; why doesn't the statusbar change? (2009-11-04)
- Previous by thread: Re: event handler in <body> doesn't work
- Next by thread: Re: event handler in <body> doesn't work
- Index(es):
Relevant Pages
|