473,225 Members | 1,247 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

Strange onkeydown behaviour (works with alert, NOT without)

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>
Jul 20 '05 #1
11 1806

"F. Da Costa" <da*****@xs4all.nl> schreef in bericht
news:3f*********************@news.xs4all.nl...

var isIE = navigator.userAgent.indexOf('MSIE') > 1;
var isMoz = navigator.userAgent.indexOf('Mozilla/5.') == 0;
Don't use the userAgent string for browser detection, but test for available
objects/functions instead:

var isIE = document.all;
var isMoz = document.getElementById && !document.all;
oTextbox.setSelectionRange(iStart + sText.length, iStart +

sText.length);

This should be:
oTextbox.setSelectionRange(iStart, iStart + sText.length);
JW

Jul 20 '05 #2
In article <3f*********************@news.wanadoo.nl>, "Janwillem Borleffs"
<jw@jwscripts.com> writes:
var isIE = document.all;
IE is not the only browser that can pass that test. Opera can pass it (in IE
spoof mode), as well as one that Jim Ley mentioned a while back (can't remember
the name of it), that supports both document.all and document.layers
var isMoz = document.getElementById && !document.all;


Opera can pass that test.

var mightBeMoz = document.getElementById && !document.all && !window.opera;

But its still better than attempted browser detection :)

var useGEBI = document.getElementById;
var useAll = document.all;
var useLayers = document.layers;

if (useGEBI){

}
else{
if (useAll){

}
else{
if (useLayers){

}else{
alert('You cant do that!')
}
}
}

I might have mismatched { } in there, but the idea is there :)

Ain't object detection grand?
--
Randy
Jul 20 '05 #3

"HikksNotAtHome" <hi************@aol.com> schreef in bericht
news:20***************************@mb-m05.aol.com...
In article <3f*********************@news.wanadoo.nl>, "Janwillem Borleffs"
<jw@jwscripts.com> writes:
var isIE = document.all;
IE is not the only browser that can pass that test. Opera can pass it (in

IE spoof mode), as well as one that Jim Ley mentioned a while back (can't remember the name of it), that supports both document.all and document.layers


You are right, but I believe that document.all represents a function rather
then a property, so:

var isOpera = document.all && 'function' == typeof document.all;

would evaluate for Opera without using the window.opera property.
JW

Jul 20 '05 #4
"HikksNotAtHome" <hi************@aol.com> wrote in message
news:20***************************@mb-m05.aol.com...
<snip>
var isIE = document.all;
IE is not the only browser that can pass that test. Opera can
pass it (in IE spoof mode), as well as one that Jim Ley mentioned
a while back (can't remember the name of it), that supports both
document.all and document.layers


Along with Konqueror (so probably Safari), IceBrowser, Web Browser 2.0
and so on, indeed there are probably more browsers these days that
support document.all than don't.

<snip> But its still better than attempted browser detection :)

var useGEBI = document.getElementById;
var useAll = document.all;
var useLayers = document.layers;

if (useGEBI){

}
else{
if (useAll){ <snip> Ain't object detection grand?


Fair enough, but the OPs decision making is related to, for example,
document.selection so the specific replacement for the next to useless -
isIE - test may be more along the lines of:-

var useDocSelection = ((document.selection)&&
(document.selection.createRange));

with additional test to verify that any created Range object has the
required - collapse - and - select - method prior to attempting to use
them. But the "Mozilla" branch would need to make a more direct test of
the features of the - oTextbox - object to see if it supported the
features required by that branch. Certainly support for -
document.getelementById - in the absence of - document.all - says
nothing about the ability to call - setSelectedRange - on the -
Textbox - object.

Richard.
Jul 20 '05 #5
Janwillem Borleffs wrote:
"F. Da Costa" <da*****@xs4all.nl> schreef in bericht
news:3f*********************@news.xs4all.nl...
var isIE = navigator.userAgent.indexOf('MSIE') > 1;
var isMoz = navigator.userAgent.indexOf('Mozilla/5.') == 0;

Don't use the userAgent string for browser detection, but test for available
objects/functions instead:

var isIE = document.all;
var isMoz = document.getElementById && !document.all;

Point taken, but it does not influence the issue as described in any way.
oTextbox.setSelectionRange(iStart + sText.length, iStart +
sText.length);

This should be:
oTextbox.setSelectionRange(iStart, iStart + sText.length);

Noop, because the first part of th string should *not* be selected.
However, this also does *not* cover the issue at hand.

JW

Thx anyway,
F DCG
Jul 20 '05 #6
F. Da Costa wrote:
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) {
var oldText = oTextbox["value"];
var iSelEnd = oTextbox["selectionEnd"];
var iStart = oTextbox["selectionStart"];
oTextbox.value = "";
// === THE FOLLOWING DOES NOT WORK AS EXPECTED ===
// ** UNCOMMENT THE ALERT AND THE CODE WORKS WITHOUT IT, IT DOESN'T
//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>
Jul 20 '05 #7
Janwillem Borleffs wrote:
"HikksNotAtHome" <hi************@aol.com> schreef in bericht
news:20***************************@mb-m05.aol.com...
It is called attribution _line_, not attribution novel.
Those who know what a message ID is, do know where to
find it.
[...] "Janwillem Borleffs" [...] writes:
var isIE = document.all;


IE is not the only browser that can pass that test. Opera can pass
it (in IE spoof mode), as well as one that Jim Ley mentioned a
while back (can't remember the name of it), that supports both
document.all and document.layers


You are right, but I believe that document.all represents a function
rather then a property, so:


Functions/methods are properties of type `function' (or `object').
var isOpera = document.all && 'function' == typeof document.all;
I prefer to use the constant value to be compared with right-hand side.
would evaluate for Opera without using the window.opera property.


It would not. Instead, it would falsely allow all UAs who support that
part of the DOM of the IE 4+ browser component to be handled as if Opera
would be used.
PointedEars

P.S.:
Your UA (Outlook Express) is borken which makes your postings hardly
leagible. Please use OE Quotefix, a similar tool or simply a working UA.
Jul 20 '05 #8

"Thomas 'PointedEars' Lahn" <Po*********@web.de> schreef in bericht
news:3F**************@PointedEars.de...

Your UA (Outlook Express) is borken which makes your postings hardly
leagible. Please use OE Quotefix, a similar tool or simply a working UA.


What is broken about my UA??????????
Jul 20 '05 #9
Janwillem Borleffs wrote:
"Thomas 'PointedEars' Lahn" <Po*********@web.de> schreef in bericht
news:3F**************@PointedEars.de...

Your UA (Outlook Express) is borken which makes your postings hardly
leagible. Please use OE Quotefix, a similar tool or simply a working
UA.


What is broken about my UA??????????


Is this better?
JW

Jul 20 '05 #10
Janwillem Borleffs wrote:
Janwillem Borleffs wrote:
"Thomas 'PointedEars' Lahn" <Po*********@web.de> schreef in bericht
news:3F**************@PointedEars.de...

Your UA (Outlook Express) is borken which makes your postings hardly
leagible. Please use OE Quotefix, a similar tool or simply a working ^^
Sorry, my bad. s/ea/e/
UA.


[...]


Is this better?


Yes, it is.
F'up2 poster

PointedEars
Jul 20 '05 #11
JRS: In article <3f*********************@news.wanadoo.nl>, seen in
news:comp.lang.javascript, Janwillem Borleffs <jw@jwscripts.com> posted
at Sat, 27 Dec 2003 14:44:03 :-

"Thomas 'PointedEars' Lahn" <Po*********@web.de> schreef in bericht
news:3F**************@PointedEars.de...

Your UA (Outlook Express) is borken which makes your postings hardly
leagible. Please use OE Quotefix, a similar tool or simply a working UA.


What is broken about my UA??????????


Ignore him; he wants to be a dictator. The concept is discredited.

--
© John Stockton, Surrey, UK. ??*@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Check boilerplate spelling -- error is a public sign of incompetence.
Never fully trust an article from a poster who gives no full real name.
Jul 20 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Z | last post by:
I have sub-classed the TextBox. In its OnKeyDown event I can intercept key strokes, examine them, etc. When I get a certain keycode (e.g., 'A') I want to change it to another unicode key from a...
2
by: Paul Drummond | last post by:
Hi all, I am developing software for Linux Redhat9 and I have noticed some very strange behaviour when throwing exceptions within a shared library. All our exceptions are derived from...
8
by: Raoul Borges | last post by:
Hi! I have a simple IF/ELSE IF/ELSE block, and it won't work as I hoped: K_IMG_RED(), K_IMG_BLUE() are function that returns either 1 or 2 (the numbers). I use them as constants. ...
4
by: KublaiKhan | last post by:
I am trying to figure out what values are returned when keys are pressed in Mozilla & IE. Here's the code I'm using. There are no alerts in either browser. Any clues would be greatly appreciated....
3
by: euler | last post by:
why did the keydown event not fire in this simple example? <HTML> <HEAD><title>keydown_div</title> <script type="text/javascript"> function keydown() { alert("keydown"); } </script>
2
by: Iver Erling Årva | last post by:
I have come across a problem with the onKeyDown event in some of my forms. I'm using onKeyDown in <form> as a standard method to open my help screen system throughout my system, but I have...
1
by: Joe | last post by:
Hi folks, Been looking at this for the day so any ideas would be appreciated. Here's the scenario, bear with me - it looks more complicated than it is :) I have a page templating system that...
8
by: Tony Johansson | last post by:
Hello! I wonder can somebody explain when is it suitable to use these methods OnKeyUp, OnKeyDown and OnKeyPress because these raise an event. These are located in class UserControl. If these...
2
by: Roman | last post by:
Hi, I have small problem. What is wrong within this script ? It works on my PC (IE6, IE7, FF) but doesn't work on other PCs with the same configuration ? Any idea ? function...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.