Re: capturing load events

On Thu, 28 Dec 2006 14:11:34 +0100, Master Br <master@sitesbr.net> wrote:

> The correct behaviour for the load event in the
> *window.addEventListener, is to run ONLY ONCE, as Gecko does*, because:
>
> Let's review some javascript:
>
> When you use an object + dot + function it means that this function will
> be working for ONLY THAT object.

I agree that this is the behaviour a script author might expect, but
the DOM2 Events specification introduces something called "event capture"  
which means that using addEventListener on some element (such as the BODY)  
lets you see events directed to elements inside it.

Please have a look at this section of the specification:

http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventTarget-addEventListener

and you may also find this explanation useful if you are not used to  
reading W3C recommendations:
http://dev.opera.com/articles/view/event-capture-explained/


> *window.addEventListener
> *It adds an Event Listener to the Window Object. The document is the
> interactive part of the window, so when the document is loaded, it MAKES
> SENSE that it will fire an event to the window = THIS IS THE CORRECT
> BEHAVIOR.

Maybe it makes sense. If the DOM spec tells us to do something that  
doesn't make sense we still have to do it - that's generally how specs  
work.

The behaviour you expect is what you get by using "false" as the third  
argument to addEventListener. That's what you should do. Using "true"  
there means "I want to run this function for ALL such events inside this  
window/document". If that is not what you intended, just replace "true"  
with "false".

> See this other object and function:
> *document.images[0].addEventListener
> *NOW I put an Event Listener on the IMAGE object. This function will
> work on the first image in the document.
> http://dosergio.kit.net/test_LOAD_events.htm

However, since you use "true" as the third argument here in your demo  
page, this function should never fire according to the DOM spec. Again  
Opera does it correctly and Firefox is wrong according to the spec. Please  
see
http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow-capture
where it says "A capturing EventListener will not be triggered by events  
dispatched directly to the EventTarget upon which it is registered."  
Again, the behaviour you expect is achieved by using "false", creating a  
non-capturing event listener.

For a demo of a capturing load event listener effect that is broken in  
Firefox due to their bug, see:
http://hallvord.com/opera/demo/progress/

Please do not follow up this discussion on whether Opera or Firefox is  
"correct". Opera is correct, Firefox had a bug that they fixed (so you  
simply can't argue that what Firefox does is correct, the Firefox  
developers recently worked on changing it to match Opera's behaviour:  
https://bugzilla.mozilla.org/show_bug.cgi?id=331306 ) - and the problem is  
that the spec needs clarification and perhaps must change to be compatible  
with the web and Firefox's workaround against the pages relying on their  
old bugs. As I already said in a personal reply please let's not digress  
into discussing what browser is "better", let's discuss what behaviour is  
more useful and how best to solve the problem with the web and the spec  
being out of tune.

Finally, I've been asked to use the list public-webapi@w3.org for this  
discussion so please reply to that list and not www-dom@w3.org .

-- 
Hallvord R. M. Steen
Core QA JavaScript tester, Opera Software
http://www.opera.com/
Opera - simply the best Internet experience

Received on Thursday, 28 December 2006 13:53:14 UTC