3 problems. All IE6. Bubbling. Source Order. Repeating

V

vbgunz

I tested the following script in Konqueror 3, Safari 3, Firefox 2 and
Opera 9. All browsers work as expected and all browsers are exactly
the same in there results. I have 3 peculiar problems I hope I
commented well on the following pastebin.

http://dpaste.com/hold/26276/

I did not include the HTML but did include the snippet in which this
script deals with (at the tail end of the paste). I am still very new
in learning JavaScript so there might be something I am missing
entirely. I appreciate any help on these issues regarding IE6.

Thanks!
 
P

pr

vbgunz said:
I tested the following script in Konqueror 3, Safari 3, Firefox 2 and
Opera 9. All browsers work as expected and all browsers are exactly
the same in there results. I have 3 peculiar problems I hope I
commented well on the following pastebin.

http://dpaste.com/hold/26276/

I did not include the HTML but did include the snippet in which this
script deals with (at the tail end of the paste). I am still very new
in learning JavaScript so there might be something I am missing
entirely. I appreciate any help on these issues regarding IE6.

Thank you for supplying long scripts separately via a URL, but next time
please include the questions themselves in your post so they are easier
to quote. For people who haven't visited the URL, they are these:

| problem 1 is: IE6 gets the source order execution wrong. all browsers
| start from the top and work their way down. IE does not. why?

If I understand the question correctly, you're wondering why IE doesn't
call the event handlers added in these lines:

addEvent(link, 'click', alertTarget);
addEvent(link, 'click', alertType);

in the order they appear in the script. The page at
http://msdn2.microsoft.com/en-us/library/ms536343.aspx says:

| If you attach multiple functions to the same event on the same object,
| the functions are called in random order, immediately after the
| object's event handler is called.

So IE is executing them in random order, as the documentation said it
would. Nothing "wrong" about it.

[Given this HTML snippet...]

<p id="intro-paragraph">bubble and get some info!
<a href="http://google.com" id="googleit">Google it!</a>
</p>

| problem 2 is: when clicking on the "link", all browsers bubble up
| correctly. First link.alertTarget then p.alertTarget (check the HTML
| below). IE6 fires the link.alertTarget *but* not p.alertTarget. why?

That's because window.event.srcElement gives you the name of the element
that was clicked, not the name of the element whose event handler is
intercepting the click. The event bubbles properly but the handlers both
report the originating element. If you were to use event.target instead
of event.currentTarget in Firefox, you'd see the same thing.

| problem 3 is: weird. IE6 doesn't bubble *but* link.alertTarget is now
| called twice. first type is called then alertTarget (2 times). IE6
| not only does it in reverse *but* also repeats itself. why?

I don't know what you mean "doesn't bubble" because you haven't
prevented it bubbling. The code

function cancel(e) {
e.preventDefault ?
e.preventDefault() : // FF2, K3, S3, O9
e.returnValue = false; // IE6
}

will prevent the A element executing its default action when clicked,
but it won't prevent other event handlers being called. By happy
coincidence the solution to this problem introduces a more conventional
way to write this type of conditional statement :)

function cancel(e) {
if (e.preventDefault) {
e.preventDefault();
} else if (typeof e.returnValue != "undefined") {
e.returnValue = false;
}
if (e.stopPropagation) {
e.stopPropagation();
} else if (typeof e.cancelBubble != "undefined") {
e.cancelBubble = true;
}
}

One interesting gotcha I noticed when testing this is that if
preventDefault(), stopPropagation() and cancelBubble = true are all
executed in Firefox 2.0.0.10, then preventDefault() is ignored. That was
annoying.

A couple of last things. The comments in your code indicate you think
some browser versions are executing a few things they in fact aren't.
You might find the FAQ notes on feature detection useful. Also you might
like to ensure your code passes a true or false in the final parameter
of addEventListener(). I guess the browser type-converts your undefined
'flag' variable to false, but it's better to make your own decision.

Hope that helps.
 
V

vbgunz

pr said:
Thank you for supplying long scripts separately via a URL, but next time
please include the questions themselves in your post so they are easier
to quote.

I apologize for this. You're 100% correct. I don't know what I was
thinking :( I'll include the URL to the paste again for those brave
souls interested in following up. all questions here are still in
regards to this paste http://dpaste.com/hold/26276/ .
| problem 1 is: IE6 gets the source order execution wrong. all browsers
| start from the top and work their way down. IE does not. why?
...
http://msdn2.microsoft.com/en-us/library/ms536343.aspx says:
... IE is executing them in random order, as the documentation said it
would. Nothing "wrong" about it.

I wouldn't have believed it if I hadn't seen it for myself. Every
other browser I've tested on is virtually the same and when you see
the same results in every browser tested, it is very pleasing. IE
though on the other hand never ceases to amaze me. I mean, just
because it is documented doesn't mean it's right, correct? heh, I
suppose I'll live with it as long as the majority of the sheep use IE
*but* do I have alternatives? Can I correct this? by correct, I mean,
without the random behavior?

In other words, can I assign several functions to the same event on an
object and get them to execute in some kind of predictable order in
every browser?
| problem 2 is: when clicking on the "link", all browsers bubble up
| correctly. First link.alertTarget then p.alertTarget (check the HTML
| below). IE6 fires the link.alertTarget *but* not p.alertTarget. why?

That's because window.event.srcElement gives you the name of the element
that was clicked, not the name of the element whose event handler is
intercepting the click. The event bubbles properly but the handlers both
report the originating element. If you were to use event.target instead
of event.currentTarget in Firefox, you'd see the same thing.

correct. I haven't really dived into everything the event object has
to offer or how exactly bubbling/capturing works. there is still much
more to it I need to understand. again, I just find it depressing how
every other browser tested works the way the books say they should and
IE being the special-needs child of them all. your tip on
stopPropagation/cancelBubble is a great tip. I haven't gotten to it
yet but already admire it's use. is it bad practice to avoid bubbling/
capturing all together by using them?

on the other hand and to be honest, I'd like to see bubbling act the
same in all browsers. It is nice to see it turned off in all (that's
the same) *but* turning it on?. any way to get all browsers or shall I
say IE to work the way all others do? e.g., click A, deal with A >
bubble up to P, deal with P?
| problem 3 is: weird. IE6 doesn't bubble *but* link.alertTarget is now
| called twice. first type is called then alertTarget (2 times). IE6
| not only does it in reverse *but* also repeats itself. why?

are you saying the bubbling is occurring *but* the very first object
that fires the event is the owning object of all bubbled events?
forgive my crude terminology. just, are you saying that the bubbling
is successful *but* the link is the owner of all later handlers fired?
this is insane. sorry, I know it is not your fault *but* how terrible
can IE make it? I mean, I am willing to accept a few mistakes *but*
sabotage?

If problem 3 has to deal with bubbling. I am definitely missing
something. I'll continue learning but hate half-assed learning. I tend
to forget the things I doubt too much. I tend to remember the things
that work. IE is killing my learning :(

You've mentioned some very good points and I would like to get to them
but as for problems 2 and 3, I am still a bit unclear about them.
problem 1, I guess is not wrong and is to be expected *but* that too
is an infuriating *feature*. it reminds me of a Futurama episode where
they were to count down from 10 to execute someone *but* they did it
randomly. so they were hitting numbers like negative 32 just to get to
0... I guess the problem is worth the memory...

thanks pr, I appreciate your help!
 
P

pr

vbgunz said:
Can I correct [IE calling duplicate event handlers in random order]?

Since in working code you would strive to register only one event
handler per event per object, the issue wouldn't come up.
In other words, can I assign several functions to the same event on an
object and get them to execute in some kind of predictable order in
every browser?

For something simple, it's easier to put the functionality in the event
handler itself. Otherwise, have your event handler retrieve functions
from a separate array or object. Or wrap the functionality in an object
and use one of its methods as the event handler.
is it bad practice to avoid bubbling/capturing ?

It's unnecessary without a reason to do so.
on the other hand and to be honest, I'd like to see bubbling act the
same in all browsers.

If you're not using event capturing, then it *does* act the same in all
the browsers we've discussed, except for the semantics. You have the
wrong end of the stick somewhere.
are you saying [wildly inaccurate paraphrase]

No. :)

Try this snippet in IE using your original HTML to see if it makes
things clearer:

window.onload = function () {
var w = window, d = document;

function aClick() {
alert("googleit received event originating at " +
w.event.srcElement.id);
}

function pClick() {
alert("intro-paragraph received event originating at " +
w.event.srcElement.id);
}

d.getElementById("googleit").attachEvent("onclick", aClick);
d.getElementById("intro-paragraph").attachEvent("onclick", pClick);
};


also try reading these:

http://developer.mozilla.org/en/docs/DOM:event#Introduction
http://msdn2.microsoft.com/en-us/library/ms533023.aspx
http://www.w3.org/TR/DOM-Level-2-Events
 
T

Thomas 'PointedEars' Lahn

pr said:
vbgunz said:
Can I correct [IE calling duplicate event handlers in random order]?

Since in working code you would strive to register only one event
handler per event per object, the issue wouldn't come up.

Not quite. It is illogical not to make use of event bubbling when that
is interoperable. Therefore, for certain events you need only one event
listener per event, that is shared through event bubbling by one element
and its descendants.
In other words, can I assign several functions to the same event on an
object and get them to execute in some kind of predictable order in
every browser?

For something simple, it's easier to put the functionality in the event
handler itself. [...]

You mean an event _listener_, as the event handler cannot be modified.
It's unnecessary without a reason to do so.

ACK


PointedEars
 
V

vbgunz

pr said:
Can I correct [IE calling duplicate event handlers in random order]?

Since in working code you would strive to register only one event
handler per event per object, the issue wouldn't come up.

well, I started out learning object.onclick = function. I understood
only one handler per event on any object using this method. I accepted
it until I learned about addEventListener/attachEvent and *if*
multiple handlers may be assigned to a single event, I figured there
has to be a good reason for it and it is reflected in my current
mindset. in other words, if there's a reason for it, I should learn
it.
For something simple, it's easier to put the functionality in the event
handler itself. Otherwise, have your event handler retrieve functions
from a separate array or object. Or wrap the functionality in an object
and use one of its methods as the event handler.

yes. perhaps a queue would fit this best. again though, I am only
learning and have no real need to produce any production code. there
is no emergency. this is all about learning and I've learned so far IE
should die a terrible death. OK, maybe I need to deal with it so I'll
learn some patience :)
is it bad practice to avoid bubbling/capturing ?

It's unnecessary without a reason to do so.
on the other hand and to be honest, I'd like to see bubbling act the
same in all browsers.

If you're not using event capturing, then it *does* act the same in all
the browsers we've discussed, except for the semantics. You have the
wrong end of the stick somewhere.
are you saying [wildly inaccurate paraphrase]

I apologize. the code you've so graciously shared in your post though
is exactly what I was thinking. sorry for any confusion. I meant, when
a handler fires, all remaining handlers in the bubble chain deal with
the originating event object as the current event object?

So, IE6 did bubble. IE6 also doesn't have an odd bug that fires the
<link> handler twice. <link> is firing and so is <p>, I am just seeing
<link> take over as the event object for what I *thought* should have
been <p>. in IE6, this is actually considered normal and I didn't do
anything wrong? I hope I made sense :)

If I am correct, you've answered all of my questions. I really cannot
thank you enough. I thought, I would be up the creek without a paddle
on this issue. I am really glad you've taken your time to help! I
really appreciate it!

I have one new question related to all of this. can you check to see
if an object has been assigned a *specific* listener/handler? I mean,
can you check if an object has say attachEvent('onclick', funcX)? I
really doubt it cause I had an idea and google couldn't help. heh,
this is probably old news or something but I'll say what I thought. I
figured to get the correct (or similar to all the rest) event object
in IE6, I could build a queue of parentNodes that contained the exact
same event. this way, I could swap out the originating event object in
the chain with the parentNode next in queue. probably makes no sense
and is probably the dumbest thing I could come up with but I thought
it was worth a shot.

still learning :)
 
V

vbgunz

Thomas said:
In other words, can I assign several functions to the same event on an
object and get them to execute in some kind of predictable order in
every browser?

For something simple, it's easier to put the functionality in the event
handler itself. [...]

You mean an event _listener_, as the event handler cannot be modified.

sorry. I am a little rusty on the definition of listener and handler.
I thought a handler was the function assigned to an event (to handle
it) and the listener was addEventListener/attachEvent (the act of what
event to listen for). am I correct? please correct me if I am
mistaken.

2 part question. does ACK mean acknowledged? what I meant by the
question is, should bubbling be disabled by default and enabled only
when needed OR is it common practice to *avoid* it in which case, it
will not hurt to disable it? is bubbling a cornerstone, a heavily used
design?

my main focus is getting the same results across 5 different browsers.
6 if you include IE7 *but* that's a browser I have no way to easily
test atm :(

thank you PointedEars
 
T

Thomas 'PointedEars' Lahn

vbgunz said:
Thomas said:
In other words, can I assign several functions to the same event on
an object and get them to execute in some kind of predictable
order in every browser?
For something simple, it's easier to put the functionality in the
event handler itself. [...]
You mean an event _listener_, as the event handler cannot be modified.

sorry. I am a little rusty on the definition of listener and handler. I
thought a handler was the function assigned to an event (to handle it)
and the listener was addEventListener/attachEvent (the act of what event
to listen for). am I correct? please correct me if I am mistaken.

An event handler is a built-in "launching pad" of the implementation that
serves for handling an event by calling attached event listeners, i.e.
user-defined callable objects (in ECMAScript implementations: Function
objects). For example, `onclick' is an event handler for the `click' event.

In DOM Level 0 and HTML 4.01, an event handler can only be assigned one
event listener (literally, in an AssignmentExpression) or by a definition of
an intrinsic event handler attribute value).

A (not recommended) mixed DOM Level 0 to 2 example, using a (proprietary)
event-listener-to-event-handler assignment:

// DOM Level 1+
var o = document.getElementById("foo");

// DOM Level 0
o.onclick = function()
{
window.alert(this.id);
};

Following the corresponding code in HTML 4.01, using a (standardized)
intrinsic event handler attribute. The attribute value determines the code
of the (primary) event listener:

<... id="foo" onclick="window.alert(this.id);">

(I left in the `id' property and HTML attribute only for the more
explanatory example; it is not required in real code. Furthermore, a
frequent beginner's mistake is to determine the causing element's object
reference by passing `this.id' to document.getElementById() in the listener;
that is unnecessary -- with element objects you have the reference with
`this' already.)

In DOM Level 2+ Events and the MSHTML DOM, an event handler would be used by
the DOM implementation to call all the event listeners added/attached for
the event and the object; in the MSHTML DOM, with attachEvent(), in
arbitrary order.
2 part question. does ACK mean acknowledged?

Correct. It is a jargon expression for understanding, in its different
meanings.
what I meant by the question is, should bubbling be disabled by default
and enabled only when needed

You can not enable or disable event bubbling *by default*.
OR is it common practice to *avoid* it in which case, it will not hurt to
disable it?

If you prevent the event from bubbling, *with your code*, you are directly
responsible for the possible consequences of your doing. What those
consequences are, depends on the actual case.

Suffice it to say that there is no need to prevent the event from bubbling
if there is no other object up the document tree to have a non-capturing
listener defined for that event. (If it is a capturing listener or an
object down the document tree, preventing the event from bubbling elsewhere
does not make any difference.)
is bubbling a cornerstone, a heavily used design?

It is where the event is known to bubble in the target DOMs and where
more than one element is involved. How much it is used then depends
on the variability of the actual case, and the level of competence of
the responsible developer(s).
my main focus is getting the same results across 5 different browsers. 6
if you include IE7 *but* that's a browser I have no way to easily test
atm :(

I noticed that you use the proprietary `window.onload' event handler
property. You should not do that; <body ... onload="listenerFunction()"> is
more reliable.

You appear to handle the `click' event. Reference material and tests
indicate that it bubbles in the W3C DOM and the MSHTML DOM. Here are
the DOM Event relations for your target environments:

IE 6/7: MSHTML DOM
Firefox 2: Gecko DOM < W3C DOM Level 3 Events
Opera 9: Opera DOM < W3C DOM Level 2 Events
Konqueror 3: KHTML DOM < W3C DOM Level 3 Events
Safari 3: WebKit DOM < W3C DOM Level 3 Events
thank you PointedEars

You're welcome.


PointedEars
 
V

vbgunz

Thomas said:
vbgunz said:
Thomas said:
In other words, can I assign several functions to the same event on
an object and get them to execute in some kind of predictable
order in every browser?
For something simple, it's easier to put the functionality in the
event handler itself. [...]
You mean an event _listener_, as the event handler cannot be modified.

sorry. I am a little rusty on the definition of listener and handler. I
thought a handler was the function assigned to an event (to handle it)
and the listener was addEventListener/attachEvent (the act of what event
to listen for). am I correct? please correct me if I am mistaken.

An event handler is a built-in "launching pad" of the implementation that
serves for handling an event by calling attached event listeners, i.e.
user-defined callable objects (in ECMAScript implementations: Function
objects). For example, `onclick' is an event handler for the `click' event.

I find my memory works better with good analogy. an event handler is
like a phone number and the listener would be on the receiving end?
e.g., addEventListener('onfire', 'nine11');. when there is a fire
(event), you call 'nine11' (event handler) and the listener takes it
from there?
You can not enable or disable event bubbling *by default*.

I understand there is no switch for this. what I meant was based on
what pr showed in his earlier response, in regards to stopPropagation,
cancelBubble. I am going to learn how to bubble because where there is
a fire, the FBI (parentNode) and the NSA (FBI.ParentNode) need to be
notified about possible terrorism.

It's just procedure man... Don't taze me bro!
I noticed that you use the proprietary `window.onload' event handler
property. You should not do that; <body ... onload="listenerFunction()"> is
more reliable.

I can get into a bunch of questions here but will cease here because I
haven't really gotten any further than window.onload :( atm, I just do
not want to get too far ahead of myself. I find focusing another great
learning tool. I will look further into this when time permits. I am
hitting the books right after this post so I may bump into it soon
enough :)

Thank you again PointedEars, always helping. I appreciate it very much!
 
V

vbgunz

vbgunz said:
Thomas said:
vbgunz said:
Thomas 'PointedEars' Lahn wrote:
In other words, can I assign several functions to the same event on
an object and get them to execute in some kind of predictable
order in every browser?
For something simple, it's easier to put the functionality in the
event handler itself. [...]
You mean an event _listener_, as the event handler cannot be modified.

sorry. I am a little rusty on the definition of listener and handler. I
thought a handler was the function assigned to an event (to handle it)
and the listener was addEventListener/attachEvent (the act of what event
to listen for). am I correct? please correct me if I am mistaken.

An event handler is a built-in "launching pad" of the implementation that
serves for handling an event by calling attached event listeners, i.e.
user-defined callable objects (in ECMAScript implementations: Function
objects). For example, `onclick' is an event handler for the `click' event.

I find my memory works better with good analogy. an event handler is
like a phone number and the listener would be on the receiving end?
e.g., addEventListener('onfire', 'nine11');. when there is a fire
(event), you call 'nine11' (event handler) and the listener takes it
from there?
You can not enable or disable event bubbling *by default*.

I understand there is no switch for this. what I meant was based on
what pr showed in his earlier response, in regards to stopPropagation,
cancelBubble. I am going to learn how to bubble because where there is
a fire, the FBI (parentNode) and the NSA (FBI.ParentNode) need to be
notified about possible terrorism.

It's just procedure man... Don't taze me bro!
I noticed that you use the proprietary `window.onload' event handler
property. You should not do that; <body ... onload="listenerFunction()"> is
more reliable.

I can get into a bunch of questions here but will cease here because I
haven't really gotten any further than window.onload :( atm, I just do
not want to get too far ahead of myself. I find focusing another great
learning tool. I will look further into this when time permits. I am
hitting the books right after this post so I may bump into it soon
enough :)

Thank you again PointedEars, always helping. I appreciate it very much!

this is funny. I scrolled back up in my current book "Wrox
Professional JavaScript" yesterday and as I was scrolling back down to
recapture my position, I came across something I already read. this
will sum up my confusion a bit (page 266).

"Event Handlers/Listeners
Events are certain actions performed either by the user or by the
browser itself. These events have names like click, load, and
mouseover. A function that is called in response to an event is called
an event handler (or, as the DOM describes it, an event listener). A
function responding to a click event is considered an onclick event
handler. Traditionally, event handlers are assigned in one of two
ways: in JavaScript or in HTML."

could this be why comp.lang.javascript seems to be so adamantly
against books for learning JS? heh, I thought it was interesting :)
 
T

Thomas 'PointedEars' Lahn

vbgunz said:
this is funny. I scrolled back up in my current book "Wrox
Professional JavaScript" yesterday and as I was scrolling back down to
recapture my position, I came across something I already read. this
will sum up my confusion a bit (page 266).

"Event Handlers/Listeners
Events are certain actions performed either by the user or by the
browser itself. These events have names like click, load, and
mouseover. A function that is called in response to an event is called
an event handler (or, as the DOM describes it, an event listener). A
function responding to a click event is considered an onclick event
handler. Traditionally, event handlers are assigned in one of two
ways: in JavaScript or in HTML."

could this be why comp.lang.javascript seems to be so adamantly
against books for learning JS? heh, I thought it was interesting :)

Yes, it is. The author evidently has not understood that events, event
handlers and event listeners are always part of a AOM or DOM, and they also
have proven not to understand that the W3C DOM (which is misdirectingly
referred there as "the DOM") is by far not the only DOM available.

Just another book that can safely be recommended against. Thanks you for
mentioning it.


PointedEars
 
T

Thomas 'PointedEars' Lahn

vbgunz said:
Thomas said:
vbgunz said:
Thomas 'PointedEars' Lahn wrote:
In other words, can I assign several functions to the same event on
an object and get them to execute in some kind of predictable
order in every browser?
For something simple, it's easier to put the functionality in the
event handler itself. [...]
You mean an event _listener_, as the event handler cannot be modified.
sorry. I am a little rusty on the definition of listener and handler. I
thought a handler was the function assigned to an event (to handle it)
and the listener was addEventListener/attachEvent (the act of what event
to listen for). am I correct? please correct me if I am mistaken.
An event handler is a built-in "launching pad" of the implementation that
serves for handling an event by calling attached event listeners, i.e.
user-defined callable objects (in ECMAScript implementations: Function
objects). For example, `onclick' is an event handler for the `click' event.

I find my memory works better with good analogy. an event handler is
like a phone number and the listener would be on the receiving end?
e.g., addEventListener('onfire', 'nine11');. when there is a fire
(event), you call 'nine11' (event handler) and the listener takes it
from there?

It would be rather something along

addEventListener('fire', nine11, false);

Nevertheless, I find yours a most fitting analogy for what happens; thank
you for it. Using this picture, it is very easy to explain that as long
nobody takes up the phone on the receiving side, nothing of importance
regarding th event will happen there. However, the event still happens and
is handled (the number is still dialed and the receiver's phone still rings).
I understand there is no switch for this. what I meant was based on
what pr showed in his earlier response, in regards to stopPropagation,
cancelBubble.

To sum it up, it does not make sense to prevent the propagation of the event
with any of the above means when there is no apparent need for that.
I am going to learn how to bubble because where there is
a fire, the FBI (parentNode) and the NSA (FBI.ParentNode) need to be
notified about possible terrorism.

It's just procedure man... Don't taze me bro!

I'll come to like your analogies for their colorfulness *g*
Thank you again PointedEars, always helping. I appreciate it very much!

:)


Regards,

PointedEars
 
P

pr

vbgunz said:
I have one new question related to all of this. can you check to see
if an object has been assigned a *specific* listener/handler? I mean,
can you check if an object has say attachEvent('onclick', funcX)? I
really doubt it cause I had an idea and google couldn't help. heh,
this is probably old news or something but I'll say what I thought. I
figured to get the correct (or similar to all the rest) event object
in IE6, I could build a queue of parentNodes that contained the exact
same event. this way, I could swap out the originating event object in
the chain with the parentNode next in queue. probably makes no sense
and is probably the dumbest thing I could come up with but I thought
it was worth a shot.

You can use obj.getAttribute('onclick') or obj.onclick == f to determine
what code has been assigned using HTML or the DOM. Beware that IE may
return an anonymous function object (not text) in that getAttribute() call.

I'm pretty sure you can't determine which eventListeners you have added
to an object, but since your script would normally keep track of which
ones it registered, the question shouldn't arise.
 
P

pr

Thomas said:
vbgunz said:
Thomas said:
In other words, can I assign several functions to the same event on
an object and get them to execute in some kind of predictable
order in every browser?
For something simple, it's easier to put the functionality in the
event handler itself. [...]
You mean an event _listener_, as the event handler cannot be modified.
sorry. I am a little rusty on the definition of listener and handler. I
thought a handler was the function assigned to an event (to handle it)
and the listener was addEventListener/attachEvent (the act of what event
to listen for). am I correct? please correct me if I am mistaken.

An event handler is a built-in "launching pad" of the implementation that
serves for handling an event by calling attached event listeners, i.e.
user-defined callable objects (in ECMAScript implementations: Function
objects). For example, `onclick' is an event handler for the `click' event.

In DOM Level 0 and HTML 4.01, an event handler can only be assigned one
event listener (literally, in an AssignmentExpression) or by a definition of
an intrinsic event handler attribute value).

A (not recommended) mixed DOM Level 0 to 2 example, using a (proprietary)
event-listener-to-event-handler assignment:

// DOM Level 1+
var o = document.getElementById("foo");

// DOM Level 0
o.onclick = function()
{
window.alert(this.id);
};

Following the corresponding code in HTML 4.01, using a (standardized)
intrinsic event handler attribute. The attribute value determines the code
of the (primary) event listener:

<... id="foo" onclick="window.alert(this.id);">

(I left in the `id' property and HTML attribute only for the more
explanatory example; it is not required in real code. Furthermore, a
frequent beginner's mistake is to determine the causing element's object
reference by passing `this.id' to document.getElementById() in the listener;
that is unnecessary -- with element objects you have the reference with
`this' already.)

In DOM Level 2+ Events and the MSHTML DOM, an event handler would be used by
the DOM implementation to call all the event listeners added/attached for
the event and the object; in the MSHTML DOM, with attachEvent(), in
arbitrary order.

If you have a source for that rather strict interpretation, I'd like to
read it. It's common for people to refer to an 'event handler function'
(abbreviated to 'event handler') as the code or function designated to
receive events using an HTML on[something] attribute or a DOM
on[something] property. It's not uncommon (though, I agree, confusing)
to see people refer to any kind of event listener as an 'event handler'.
 
T

Thomas 'PointedEars' Lahn

pr said:
Thomas said:
In DOM Level 0 and HTML 4.01, an event handler can only be assigned one
event listener (literally, in an AssignmentExpression) or by a definition of
an intrinsic event handler attribute value).
[...]
In DOM Level 2+ Events and the MSHTML DOM, an event handler would be used by
the DOM implementation to call all the event listeners added/attached for
the event and the object; in the MSHTML DOM, with attachEvent(), in
arbitrary order.

If you have a source for that rather strict interpretation, I'd like to
read it.

I don't have one (besides the fact that you don't find "event handler" in
the W3C DOM Event Specifications). Try common sense: The event and the
event handler are features of the DOM. The event listener itself is not, as
that is user-defined code. The DOM only provides for the possibility that
this user-defined code be called by what is executed on event; the latter
must be the event handler.
It's common for people to refer to an 'event handler function' (abbreviated
to 'event handler') as the code or function designated to receive events
using an HTML on[something] attribute or a DOM on[something] property. It's
not uncommon (though, I agree, confusing) to see people refer to any kind
of event listener as an 'event handler'.

People, especially incompetent ones, give things all kinds of inappropriate
names.

Please trim your quotes.


PointedEars
 
V

vbgunz

Thomas said:
I noticed that you use the proprietary `window.onload' event handler
property. You should not do that; <body ... onload="listenerFunction()"> is
more reliable.

this is something I promised to get to as soon as time permitted. I
thought you were referring to the infamous addLoadEvent method here.
but coming back, I am beginning to think you probably meant something
else? Anyhow, I knew of addLoadEvent but disregarded it in favor of
window.onload (so much simpler). I went back to the book in which I
first heard about addLoadEvent and tried to really figure it out. It
was an aha! moment for sure and I love it. is this what you were
referring too?

if so I understand it now and will definitely use it!

thank you for your time!
 
T

Thomas 'PointedEars' Lahn

vbgunz said:
this is something I promised to get to as soon as time permitted. I
thought you were referring to the infamous addLoadEvent method here.
but coming back, I am beginning to think you probably meant something
else?

I was referring to the code you provided, starting with the first line;
I don't see an addLoadEvent() method there, nor is one required.
Anyhow, I knew of addLoadEvent [...]

What are you talking about? There is no such predefined method in
the known host environments.


PointedEars
 
V

vbgunz

Thomas said:
vbgunz said:
this is something I promised to get to as soon as time permitted. I
thought you were referring to the infamous addLoadEvent method here.
but coming back, I am beginning to think you probably meant something
else?

I was referring to the code you provided, starting with the first line;
I don't see an addLoadEvent() method there, nor is one required.
Anyhow, I knew of addLoadEvent [...]

What are you talking about? There is no such predefined method in
the known host environments.

addLoadEvent is a function created by Simon Willison and here is the
birth page and explanation of it http://simonwillison.net/2004/May/26/addLoadEvent/
.. I first found out about this function through the Friends of Ed, DOM
Scripting book. I found window.onload = easier at the time. going back
to the book based on cause of what you mentioned, I now understand and
prefer this function over window.onload. this is what I thought you
meant. maybe not exactly known by the name of addLoadEvent *but*
something similar.

I usually nest all my code within window.onload. not exactly because I
need it but I find while learning it is the easiest way to test what I
am working on. If working with window.onload is not exactly wise, what
would you suggest? would you mind clearing up how best to work with
window.onload? e.g., addEventListener?
 
T

Thomas 'PointedEars' Lahn

vbgunz said:
Thomas said:
vbgunz said:
Thomas 'PointedEars' Lahn wrote:
I noticed that you use the proprietary `window.onload' event handler
property. You should not do that; <body ... onload="listenerFunction()"> is
more reliable.
this is something I promised to get to as soon as time permitted. I
thought you were referring to the infamous addLoadEvent method here.
but coming back, I am beginning to think you probably meant something
else?
I was referring to the code you provided, starting with the first line;
I don't see an addLoadEvent() method there, nor is one required.
Anyhow, I knew of addLoadEvent [...]
What are you talking about? There is no such predefined method in
the known host environments.

addLoadEvent is a function created by Simon Willison and here is the
birth page and explanation of it http://simonwillison.net/2004/May/26/addLoadEvent/

Apparently he's one of the those who don't understand that this approach is
inherently unreliable, needlessly. I am disappointed to see PPK supporting
the abolishment of intrinsic event handler attributes in favor of this
approach as I got the impression that his other contributions tell of a more
thorough thinking regarding client-side Web development.
would you mind clearing up how best to work with
window.onload? e.g., addEventListener?

Neither of those. The intrinsic `onload' event handler *attribute* of the
`body' element is always the best bet.


PointedEars
 

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,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top