Hi,
This is a strange issue I'v been staring at for half a day now.
It concerns catching keys via the onkeydown handler.
In IE5+ it works fine but in Moz 1.6 (& Firebird 0.7+) it behaves most
peculiar. The 'offensive' code is included below.
The weird thing lies in the alert box preceded by // **.
* function textboxReplaceS elect *
If the alert is active it works perfect. No key duplication in the textbox
on the screen.
insert the comment ()i.e. take out the alert) and one gets two characters
instead of one.
It almost looks like something 'shoots through' because (to make things
worse) the last alert shows me a singular entry as well.
To test the code just use the following
TIA
Fermin DCG
<html>
<head>
<script>
var isIE = navigator.userA gent.indexOf('M SIE') > 1;
var isMoz = navigator.userA gent.indexOf('M ozilla/5.') == 0;var _character;
var _keyCode;
var _charCode;
// decipher key down codes
function showDown(oTextb ox, evt) {
_keyCode = _charCode = _character = null;
evt = (evt) ? evt : ((event) ? event : null);
if (evt) {
_keyCode = evt.keyCode;
if (evt.charCode) _charCode = evt.charCode;
else _charCode = (isIE) ? _keyCode : ((_charCode) ? _charCode : _keyCode);
_character = String.fromChar Code(_charCode) .toLowerCase();
}
textboxReplaceS elect(oTextbox, _character);
return false;
}
/** THE OFFENSIVE CODE
* Replace the currently selected text with some other text
* F.e.: when a <x> is pressed in an empty textbox it should come out with
1 x only on the screen.
* @param oTextbox = the textbox to act on
* @param sText = the text to insert
*/
function textboxReplaceS elect (oTextbox, sText) {
if (isIE) {
var oRange = document["selection"].createRange();
oRange.text = sText;
oRange.collapse (true);
oRange.select() ;
} else if (isMoz) { // THIS DOES NOT WORK AS EXPECTED
var oldText = oTextbox["value"];
var iSelEnd = oTextbox["selectionE nd"];
var iStart = oTextbox["selectionStart "];
oTextbox.value = "";
// ** UNCOMMENT AND THE CODE WORKS
//alert("1.textbo xReplaceSelect: :"+oldText+" - "+sText+" - "+
oTextbox.value) ;
//oTextbox.value = oTextbox.value. substring(0, iStart) + sText +
oTextbox.value. substring(oText box.selectionEn d, oTextbox.value. length);
oTextbox.value = oldText.substri ng(0, iStart) + sText +
oldText.substri ng(iSelEnd, oldText.length) ;
//alert("2.textbo xReplaceSelect: :"+oTextbox.val ue+" - "+sText);
oTextbox.setSel ectionRange(iSt art + sText.length, iStart + sText.length);
}
oTextbox.focus( );
alert("exit.tex tboxReplaceSele ct::"+oTextbox. value+" - "+sText);
}
</script>
</head>
<body>
<input type="text" value="" id="txtSearch" onkeydown="retu rn showDown(this,
event);" />
</body>
</html>