Javascript events: keydown, keyup and change are screwy

A

Andrew DeFaria

I thought this would be fairly straight forward but apparently it's not.
Given the following html file:

<!DOCTYPE html PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>Test</title>
</head>
<body>
<form method="post" action="javascript:">
Type a key
<input
align = "right"
type = "text"
name = "field"
size = "8"
onkeydown = "alert ('keydown');"
onkeyup = "alert ('keyup');"
onchange = "alert ('change');"</form>
</body>
</html>

Pretty simple. (Browse to http://defaria.com/test.html). What one would
expect is if a key was typed that the following alerts would occur:
keydown, keyup and when you leave the field you'd get a change. Not so
with the two major browsers (well I'm using FireFox and IE).

With Firefox I get the following (click on the text input box and type a
character):

* keyup! Why not keydown first!
* change! Why a change?!? I should only get a change when I leave
the field no?
* keydown! It's about time!

Now click anywhere outside the text field to "leave" the field and...
Nothing.

Now with IE (again click on text input bos and type a character):

* keydown - cool, what I expect
* Nothing! - Hmmm what happened to the keyup?!?

Now click anywhere outside the test field to "leave" the field and:

* change: OK that's expected but again what happened to the keydown?

Anybody have any ideas? Opinions?
 
V

Vincent van Beveren

Anybody have any ideas? Opinions?

The problem is that when you do an alert, a seperate dialogwindow is
opened and the focus on the window handeling the events is lost. Somehow
this results in some weird behaviour. I modified your script a bit, and
now it does give the output you expect. I tested it in IE, NS7 and FireFox

<!DOCTYPE html PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>Test</title>
<script language="JavaScript">

function debug(s) {
ta = document.getElementById("debugArea");
ta.value+=s+"\n";
}

</script>
</head>
<body>
<form method="post" action="javascript:">
Type a key
<input
align = "right"
type = "text"
name = "field"
size = "8"
onkeydown = "debug ('keydown');"
onkeyup = "debug ('keyup');"
onchange = "debug ('change');"</form>
<textarea id="debugArea" readonly="true" cols="40" rows="20"></textarea>
</body>
</html>
 
L

Lee

Andrew DeFaria said:
This is a multi-part message in MIME format.
--------------020300050907030309060809
Now with IE (again click on text input bos and type a character):<br>
<ul>
<li>keydown - cool, what I expect</li>
<li>Nothing! - Hmmm what happened to the keyup?!?</li>
</ul>
Now click anywhere outside the test field to "leave" the field and:<br>
<ul>
<li>change: OK that's expected but again what happened to the keydown?</li>
</ul>
Anybody have any ideas? Opinions?<br>


Please post plain text only to this newsgroup.

I don't have Firefox, but when testing in the two major browsers
I see the results you describe for IE.

Your text element never sees the keyup event because the alert
has taken focus before it happens. That's why the keydown
event is rarely useful.
 
M

McKirahan

I thought this would be fairly straight forward but apparently it's not.
Given the following html file:

<!DOCTYPE html PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>Test</title>
</head>
<body>
<form method="post" action="javascript:">
Type a key
<input
align = "right"
type = "text"
name = "field"
size = "8"
onkeydown = "alert ('keydown');"
onkeyup = "alert ('keyup');"
onchange = "alert ('change');"</form>
</body>
</html>

Pretty simple. (Browse to http://defaria.com/test.html). What one would
expect is if a key was typed that the following alerts would occur: keydown,
keyup and when you leave the field you'd get a change. Not so with the two
major browsers (well I'm using FireFox and IE).

With Firefox I get the following (click on the text input box and type a
character):

keyup! Why not keydown first!
change! Why a change?!? I should only get a change when I leave the field
no?
keydown! It's about time!
Now click anywhere outside the text field to "leave" the field and...
Nothing.

Now with IE (again click on text input bos and type a character):

keydown - cool, what I expect
Nothing! - Hmmm what happened to the keyup?!?
Now click anywhere outside the test field to "leave" the field and:

change: OK that's expected but again what happened to the keydown?
Anybody have any ideas? Opinions?
--
Some people are like Slinkies . . not really good for anything, but you
still can't help but smile when you see one tumble down the stairs.


Try changing "alert" to "windows.status +=":

<html>
<head>
<title>onkeys.htm</title>
</head>
<body>
<form method="post" action="javascript:">
Type a key
<input
align = "right"
type = "text"
name = "field"
size = "8"
onkeydown = "window.status += ' keydown';"
onkeyup = "window.status += ' keyup';"
onchange = "window.status += ' change';"</form>
</body>
</html>
 
D

DU

Andrew said:
I thought this would be fairly straight forward but apparently it's not.
Given the following html file:

<!DOCTYPE html PUBLIC "-//w3c//dtd html 4.0 transitional//en">

Error: invalid formal public identifier -//w3c//dtd html 4.0
transitional//en: invalid public text class

W3C Quality Assurance
List of valid DTDs you can use in your document.
http://www.w3.org/QA/2002/04/valid-dtd-list.html

W3C Quality Assurance Tutorial
My Web site is standard! And yours?
http://www.w3.org/QA/2002/04/Web-Quality

Why Validate Your HTML
Creating Valid HTML Documents Means Cleaner Code and Easier Maintenance
http://webdesign.about.com/library/weekly/aa092799.htm
<html>
<head>
<title>Test</title>
</head>
<body>
<form method="post" action="javascript:">

I'm surprised you can write
action="javascript:"
just like that. Why not just
action=""
if you don't want to submit the form?
Type a key
<input
align = "right"
type = "text"
name = "field"
size = "8"
onkeydown = "alert ('keydown');"

As someone mentioned, creating an alert here is not wise.

KeyEvent:properties (that demo can be improved)
http://www.din.or.jp/~hagi3/JavaScript/JSTips/Mozilla/Samples/KeyEvent.htm
onkeyup = "alert ('keyup');"
onchange = "alert ('change');"
</form>
</body>
</html>

Pretty simple.

I recommend you always validate your markup code with a validator before
posting a question. All problems which will need to be corrected anyway
after testing with various user agents are related to validation errors.

DU
 
A

Andrew DeFaria

DU said:
Error: invalid formal public identifier -//w3c//dtd html 4.0
transitional//en: invalid public text class

Well first off this was just a little test. I try to remain compliant in
more formal web pages. Of course, they make it so difficult...
W3C Quality Assurance
List of valid DTDs you can use in your document.
http://www.w3.org/QA/2002/04/valid-dtd-list.html

Interesting list, with no real information about which one I should use
and why! HTML 2.0? 3.2? 4.01? Strict? Transitional? Frameset? What about
XHMTL? Who knows! My guess would be HTML 4.01 Strict would be best for
me and I will endeavor to use that. But perhaps the reason why people
pay that much attention to such standards and validations is that they
are not easy to do and not explained very well.
W3C Quality Assurance Tutorial
My Web site is standard! And yours?
http://www.w3.org/QA/2002/04/Web-Quality

Interesting and yada, yada. Yes standards are important. Yes I try to
comply. Yes I will try harder.
Why Validate Your HTML
Creating Valid HTML Documents Means Cleaner Code and Easier Maintenance
http://webdesign.about.com/library/weekly/aa092799.htm


I'm surprised you can write action="javascript:" just like that. Why
not just action="" if you don't want to submit the form?

I'm equally surprised that you can write action=""! IOW I think it's
largely irreverent to the issue at hand.
As someone mentioned, creating an alert here is not wise.

Yes. I got that. And I understand why now.

Thanks. But it was not a demo. It was a chopped down example of a
problem I was having.
I recommend you always validate your markup code with a validator
before posting a question. All problems which will need to be
corrected anyway after testing with various user agents are related to
validation errors.

Not so really here. The problem was the usage of alert! ;-)
 
R

Richard Cornford

Andrew DeFaria wrote:
I'm confused! ...
<snip>

| Content-Type: multipart/alternative;
| boundary="------------040701020404010203020602"

Content types for posts to comp.lang.javascript should be plain text
only.

Richard.
 
A

Andrew DeFaria

Richard said:
Andrew DeFaria wrote:


<snip>

| Content-Type: multipart/alternative;
| boundary="------------040701020404010203020602"

Content types for posts to comp.lang.javascript should be plain text only.

So says you!
 
L

Lee

Andrew DeFaria said:
Content types for posts to comp.lang.javascript should be plain text
only.<br>
</blockquote>
So says you!<br>
</blockquote>
So says the newsgroup FAQ:<br>
<a class="moz-txt-link-freetext"
href="http://www.jibbering.com/faq/#FAQ2_3">http://www.jibbering.com/faq/#FAQ2_3</a><br>
</blockquote>
BFD! Sue me!<br>

It's more or less self-enforcing. People who are so rude
as to continue to post HTML gain reputations as assholes,
and so the people who are most able to help simply stop
reading their posts.

Then there are the prospective employers who Google to see
what this "Andrew DeFaria" has contributed, and find out
that you have no respect for established conventions, or
for other people.

Best of luck in life. I think you're going to need it.
 
R

Richard Cornford

Andrew said:
Lee wrote:<br>
<blockquote cite="(e-mail address removed)"
type="cite">Andrew DeFaria said:<br>
<br>
<blockquote type="cite">Content types for posts to
comp.lang.javascript should be plain text<br>
only.&lt;br&gt;<br>
&lt;/blockquote&gt;<br>
So says you!&lt;br&gt;<br>
&lt;/blockquote&gt;<br>
So says the newsgroup FAQ:&lt;br&gt;<br>
&lt;a class="moz-txt-link-freetext"<br>
href="<a class="moz-txt-link-freetext"
href="http://www.jibbering.com/faq/#FAQ2_3">
http://www.jibbering.com/faq/#FAQ2_3</a>
"&gt;<a class="moz-txt-link-freetext"
href="http://www.jibbering.com/faq/#FAQ2_3">
http://www.jibbering.com/faq/#FAQ2_3</a>
&lt;/a&gt;&lt;br&gt;<br>
&lt;/blockquote&gt;<br>
BFD! Sue me!&lt;br&gt;<br>
</blockquote>
<!----><br>
It's more or less self-enforcing. People who are so rude as
to continue to post HTML gain reputations as assholes, and so
the people who are most able to help simply stop reading their
posts.<br>
</blockquote>
It has been my continuing experience (IOW real world) that
this is just a fallacy. In the real world it matters little to
most people, except the most pig headed (who usually have little
to contribute anyway), and that it doesn't end up making a
difference at all.<br>
<snip>

You are willing to gamble that the people who think that the established
Usenet conventions should be followed are just being "pig headed" and
"have little to contribute anyway" because "most people" wouldn't care?

Well most people (on a head-count basis) don't care, but then the advice
(reaction?) you would get from most people on a javascript/browser
scripting question would probably make you wish you hadn't bothered
asking them.
Then again, surprise, surprise, HTML <b>IS</b> a standard!
<snip>

The UK's square-pinned 13A domestic electrical plug is *a* standard, it
just isn't the standard everywhere. In most places such a plug is
useless and there are probably some places where attempting to use one
would be dangerous. Standards have contexts, in this context the
standard is plain text (and for a reason).

Still, you have been advised, you have been warned, and you have made a
decision.

Richard.
 
A

Andrew DeFaria

Richard said:
You are willing to gamble that the people who think that the
established Usenet conventions should be followed are just being "pig
headed" and "have little to contribute anyway" because "most people"
wouldn't care?

I thought I was pretty clear before. Yes.
The UK's square-pinned 13A domestic electrical plug is *a* standard,
it just isn't the standard everywhere. In most places such a plug is
useless and there are probably some places where attempting to use one
would be dangerous. Standards have contexts, in this context the
standard is plain text (and for a reason).

The good thing about standards is that there are sooo many to choose
from! And generally standards carry more weight than conventions. You're
plea was for a convention. Are you really that surprised that not
everybody gives the same weight to conventions as to standards?
Still, you have been advised, you have been warned, and you have made
a decision.

Yes so why do you go on and on about it?
 
R

rh

:

You are willing to gamble that the people who think that the established
Usenet conventions should be followed are just being "pig headed" and
"have little to contribute anyway" because "most people" wouldn't care?

Well most people (on a head-count basis) don't care, but then the advice
(reaction?) you would get from most people on a javascript/browser
scripting question would probably make you wish you hadn't bothered
asking them.


The UK's square-pinned 13A domestic electrical plug is *a* standard, it
just isn't the standard everywhere. In most places such a plug is
useless and there are probably some places where attempting to use one
would be dangerous. Standards have contexts, in this context the
standard is plain text (and for a reason).

Still, you have been advised, you have been warned, and you have made a
decision.

So is it safe to say, then, in summary?:

There are those who will choose to engage in discussion groups to
become protagonists, and conversely, there are those who will become
disengaged if they choose to be antagonists of the pro's.

Probably not. ;-)

../rh
 
R

Richard Cornford

rh said:
:

So is it safe to say, then, in summary?:

There are those who will choose to engage in discussion groups to
become protagonists, and conversely, there are those who will become
disengaged if they choose to be antagonists of the pro's.

No that would be a misguided conclusion. There is no protagonist
relationship; advice is given and the benefits of following it
documented. While the consequences of disregarding it may not be
absolute or certain, the precedents suggest that they are negative, and
in extreme cases (e.g. George Hester) render participation in the group
futile.

Usenet is a medium where all communication is one-to-many, which may
alone suggest that the onus is on the author of articles posted to
create them with a consideration for the many that will (may) read them.
Usenet posting conventions are mostly concerned with maximising the
effectiveness and efficiency of that communication for the readers of
articles (as the one-to-many relationship would suggest they should).

For occasional users of Usenet the relative inefficiency of postings
that do not conform to the conventions would not necessarily be very
apparent, but for regular users they become very apparent, particularly
when a reasonable expectation of conventional behaviour results in an
action that proves futile (such as scrolling down through a quoted block
of text to read the following response only to find that there is no
following comment). Thus the disregard for the conventions on the part
of an individual disproportionately impacts on the regular users of
Usenet, and the one-to-many relationship means that it has that impact
on many of them.

Any individual's initial ignorance of the relevant Usenet conventions is
undesirable but probably inevitable. Ignorance can be cured by directing
people to relevant reference material (the group's FAQ), and forgiven.

But a decision to disregard the conventions once informed of them is
equivalent to announcing a desire to deliberately waste the time of
everyone on the receiving end of the one-to-many relationship, and
particularly the regular participants in the group.
Probably not. ;-)

There is nothing that can be done to force people to post in any
particular way, but if someone announces their intention to try to waste
my time I can mitigate the effect by choosing not to spend my time on
their posts (not giving them my time so they cannot waste it).

Of course my reaction may not be everyone else's, which is why I
described it as a gamble. But my experience suggest that the majority of
the regular (and most informed/potentially useful) contributors to this
group, probably because incorrect posting style impacts more on regular
participants, are supporters of the observance of the conventions and
they do react to individuals how will not observe them by not answering
their questions.

Whitens the number of threads in which people are asked not to top-post,
where a top-posted response containing a trivial follow-up question
receives no response. I know the answer to the follow-up questions, I
know that 20-odd other regular contributors to the group also know the
answer, but still it isn't posted. Coincidence or reaction?

Generally, when people express an unwillingness to go out of their way
for the benefit of those around them, expecting assistance when in need
is unrealistic.

Richard.
 
D

DU

Richard said:
rh said:

[snipped]


There is nothing that can be done to force people to post in any
particular way, but if someone announces their intention to try to waste
my time I can mitigate the effect by choosing not to spend my time on
their posts (not giving them my time so they cannot waste it).

Of course my reaction may not be everyone else's, which is why I
described it as a gamble. But my experience suggest that the majority of
the regular (and most informed/potentially useful) contributors to this
group, probably because incorrect posting style impacts more on regular
participants, are supporters of the observance of the conventions and
they do react to individuals how will not observe them by not answering
their questions.

Whitens the number of threads in which people are asked not to top-post,
where a top-posted response containing a trivial follow-up question
receives no response. I know the answer to the follow-up questions, I
know that 20-odd other regular contributors to the group also know the
answer, but still it isn't posted. Coincidence or reaction?

Generally, when people express an unwillingness to go out of their way
for the benefit of those around them, expecting assistance when in need
is unrealistic.

Richard.

Richard, please move on. Some people will never budge, will never admit
defeat, will never comply with whatever standards there is for the
general/public good.

Most people come in this newsgroup hungry for answers and solutions:
don't bother them with usenet standards, reading FAQs, top-posting, or
searching in newsgroup archives or learning from online tutorials etc.
and things like that. They want answers, solutions, perfectly fitted,
manageable, digestable solutions to their belly-button problem/webpages
and anything that isn't going in that same direction is an annoyance, an
enemy, an obstacle to their goal, a stupid waste of their precious time,
etc.. Almost like road rage. Just like Homer Simpson with donuts, hot
dogs, Krusty burgers, etc. Some of them will even tell you with utmost
seriousness that the purpose of this newsgroup is to serve them and to
give them help.

FWIW, Mozilla Mail & Newsgroup client has a setting for these people:
{not Message filters :)}
but
View/Message Body As/Plain Text
I've been using it for over 2 years now and never got a problem and
rarely noticed that the poster was sending HTML messages. It's still not
the best solution but at least it gives me results I wish for.

DU
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
Most people come in this newsgroup hungry for answers and solutions:
don't bother them with usenet standards, reading FAQs, top-posting, or
searching in newsgroup archives or learning from online tutorials etc.
and things like that. They want answers, solutions, perfectly fitted,
manageable, digestable solutions to their belly-button problem/webpages
and anything that isn't going in that same direction is an annoyance, an
enemy, an obstacle to their goal, a stupid waste of their precious time,
etc.


Compliant questioners get better results; but this will only be noticed
by the more attentive regulars.

In order that worthy but ignorant non-compliant questioners can get
better results, it is necessary for proper practice to be pointed out.

Some then comply, and get what they deserve.
Some fail to comply, and also get what they deserve.
Some argue, and should then be ignored.

But one must also remember that articles of remonstration are read by
future posters other than the remonstratee; and the more sagacious of
these future posters will amend their ways in advance of posting -
therein lies the major benefit.

The late Admiral Byng should have been of considerable benefit to the
Navy, in the manner recognised by Voltaire in "Candide".
 
L

Lasse Reichstein Nielsen

DU said:
Richard, please move on. Some people will never budge, will never
admit defeat, will never comply with whatever standards there is for
the general/public good.

True, but there are also those that will change their ways when
explained why the other way is better. Since Richard's message was
very well stated, it gives people all possible chances to begin
posting according to standard and tradition.
Most people come in this newsgroup hungry for answers and solutions:
don't bother them with usenet standards, reading FAQs, top-posting, or
searching in newsgroup archives or learning from online tutorials
etc. and things like that.

It's very simple: People willing to make an effort when posting, both
form and content, are worth doing doing an effort to help. The rest
.... well, who cares?

There is not enough time to help everybody. Advising people to change
the way they quote can be as big a help as solving their current
problem, because it will also help them get their future problems
solved easier.


/L
 
S

Stanimir Stamenkov

/Andrew DeFaria/:
Again, sorry, but for the most part this doesn't happen either. Then
again, surprise, surprise, HTML *IS* a standard! Perhaps sometime in
your life you'll be able to wake up from your 60's ASCII is king, ASCII
only haze and change and see that.

What ASCII have to do with the group requirement that the messages
should be posted in plain text format and not in HTML? The plain
text format is chosen so most of the people will benefit. If you
really want to contribute - please, abide the group conventions.

BTW, you're posting your messages in multipart/alternative where you
post two versions of every message of yours - it is just unnecessary
waste.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,757
Messages
2,569,543
Members
45,026
Latest member
camilin05

Latest Threads

Top