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 textboxReplaceSelect *
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.userAgent.indexOf('MSIE') > 1;
var isMoz = navigator.userAgent.indexOf('Mozilla/5.') == 0;var _character;
var _keyCode;
var _charCode;
// decipher key down codes
function showDown(oTextbox, 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.fromCharCode(_charCode).toLowerCase();
}
textboxReplaceSelect(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 textboxReplaceSelect (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["selectionEnd"];
var iStart = oTextbox["selectionStart"];
oTextbox.value = "";
// ** UNCOMMENT AND THE CODE WORKS
//alert("1.textboxReplaceSelect::"+oldText+" - "+sText+" - "+
oTextbox.value);
//oTextbox.value = oTextbox.value.substring(0, iStart) + sText +
oTextbox.value.substring(oTextbox.selectionEnd, oTextbox.value.length);
oTextbox.value = oldText.substring(0, iStart) + sText +
oldText.substring(iSelEnd, oldText.length);
//alert("2.textboxReplaceSelect::"+oTextbox.value+" - "+sText);
oTextbox.setSelectionRange(iStart + sText.length, iStart + sText.length);
}
oTextbox.focus();
alert("exit.textboxReplaceSelect::"+oTextbox.value +" - "+sText);
}
</script>
</head>
<body>
<input type="text" value="" id="txtSearch" onkeydown="return showDown(this,
event);" />
</body>
</html>