![]() | |
![]() |
| | Thread Tools | Display Modes |
#11
| |||
| |||
|
|
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; Isn't that something. Makes you wonder what MS has against the BS key. Maybe they are trying to make it hard to break. 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. Well, that likely means that IE4 is the same story. FWIW, that's good news. In addition, I've carried out some tests with the following results: a) document.onkeyup doesn't work (obstruct 'Backspace key') for IE8, No change there. 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; We know about IE. Additionally Safari 3 won't let you cancel onkeypress with document and DOM0. c) document.onkeydown doesn't work for FF3 and Opera 9.64, but it works for Safari 3 and IE 8. Again, we know about IE. What do you mean onkeydown "doesn't work" for FF3? Regardless, the keydown event is not normally used for this. Granted, the original request doesn't require any action except to cancel the default action, so that is a bit of a moot point. 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: [snip] Or something like this:- var bsKeyEventType; var myBSCallback = function() { //console.log('Event type: ' + bsKeyEventType); }; document.onkeydown = document.onkeypress = document.onkeyup = function (evt) { evt = evt || window.event; var key = evt.which || evt.keyCode; if (key == 8) { switch (evt.type) { case 'keydown': if (!bsKeyEventType) { bsKeyEventType = 'keydown'; document.onkeypress = null; } break; case 'keypress': if (!bsKeyEventType) { bsKeyEventType = 'keypress'; document.onkeydown = null; document.onkeyup = null; } myBSCallback(); break; case 'keyup': if (!bsKeyEventType) { bsKeyEventType = 'keyup'; document.onkeydown = null; document.onkeypress = null; } myBSCallback(); } return false; } }; |
#12
| ||||||
| ||||||
|
|
On Nov 4, 12:45 am, David Mark <dmark.cins... (AT) gmail (DOT) com> wrote: 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; Isn't that something. *Makes you wonder what MS has against the BS key. *Maybe they are trying to make it hard to break. 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. Well, that likely means that IE4 is the same story. *FWIW, that's good news. In addition, I've carried out some tests with the following results: a) document.onkeyup doesn't work (obstruct 'Backspace key') for IE8, No change there. 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; We know about IE. *Additionally Safari 3 won't let you cancel onkeypress with document and DOM0. c) document.onkeydown doesn't work for FF3 and Opera 9.64, but it works for Safari 3 and IE 8. Again, we know about IE. *What do you mean onkeydown "doesn't work" for FF3? Regardless, the keydown event is not normally used for this. *Granted, the original request doesn't require any action except to cancel the default action, so that is a bit of a moot point. 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: [snip] Or something like this:- var bsKeyEventType; var myBSCallback = function() { * * * * //console.log('Event type: ' + bsKeyEventType); }; document.onkeydown = document.onkeypress = document.onkeyup = function (evt) { * * * * evt = evt || window.event; * * * * var key = evt.which || evt.keyCode; * * * * if (key == 8) { * * * * * * * * switch (evt.type) { * * * * * * * * case 'keydown': * * * * * * * * * * * * if (!bsKeyEventType) { * * * * * * * * * * * * * * * * bsKeyEventType = 'keydown'; * * * * * * * * * * * * * * * * document.onkeypress = null; * * * * * * * * * * * * } * * * * * * * * * * * * break; * * * * * * * * case 'keypress': * * * * * * * * * * * * if (!bsKeyEventType) { * * * * * * * * * * * * * * * * bsKeyEventType = 'keypress'; * * * * * * * * * * * * * * * * document.onkeydown = null; * * * * * * * * * * * * * * * * document.onkeyup = null; * * * * * * * * * * * * } * * * * * * * * * * * * myBSCallback(); * * * * * * * * * * * * break; * * * * * * * * case 'keyup': * * * * * * * * * * * * if (!bsKeyEventType) { * * * * * * * * * * * * * * * * bsKeyEventType = 'keyup'; * * * * * * * * * * * * * * * * document.onkeydown = null; * * * * * * * * * * * * * * * * document.onkeypress = null; * * * * * * * * * * * * } * * * * * * * * * * * * myBSCallback(); * * * * * * * * } * * * * * * * * return false; * * * * } }; Did you test your example with IE8 and FF3 and Safari 3 and Opera 9.64???? Sorry to tell you this, but your code doesn't work as expected on all the four browsers mentioned. |
|
Unfortunately, in this situation, events are mutually excludent depending on the browser in use; in other words: |
|
* If we're using FF3 and Opera 9.64, then the BS key must be blocked only by document.onkeypress; |
|
* If we're using IE8 and Safari, then the BS key must be blocked only by document.onkeydown. |
|
If we try a different thing, then the BS key will not be blocked as expected. |
|
I'm inclined to think that this is a case where browser sniffing might be necessary, in order to choose the correct document's event to attach our listener to. I've tested the following code successfully on the 4 browsers listed above: |
![]() |
| Thread Tools | |
| Display Modes | |
| |