Event capture explained

Forums » Dev.Opera » Archived Article Discussions

This topic has been closed. No new entries allowed.

Reason: You can now post comments on articles on Dev Opera

Forum rules and guidelines

You need to be logged in to post in the forums. If you do not have an account, please sign up first.

Go to last post

3. November 2006, 16:33:35

Opera Software

hallvors

Opera Software

Posts: 1776

Event capture explained

How to properly capture events in JavaScript.

( Read the article )

--
My blog: miscoded
Stupid code from major websites uncovered and criticised
Contribute site fixes! - OTW&TA- all sites must work

4. November 2006, 13:00:58

Opera Software

tarquinwj

Taking DHTML to level 5

Posts: 546

Implementations

addEventListener and capturing events is part of the DOM2 Events standard, which is supported by Opera, Gecko/Firefox, and Safari. IE supports the slightly different attachEvent API.



Just to add to the list;
Konqueror, iCab, NetFront and ICE browser also support DOM 2 Events. Konqueror's support is very similar to Safari. iCab, NetFront, and ICE do not support manually firing events, but in general they do the rest ok.

At the moment, pretty much everything except Opera makes the mistake of firing in the capture phase on the target. Sometimes I wonder if maybe it would be be easier to get the spec changed in DOM 3 so that firing on the target is correct. Not that I think it is right, but simply because it is what most browsers do, and it would be easier to change the spec and Opera, than to convince the other browsers, and the websites that follow their incorrect implementations, to change.

As for the issue of capturing load events, Opera's (correct) implementation is fundamentally more useful. Perhaps the other browsers mistook .bubbles for .bubblesOrCaptures even though that is not supported by the spec. Do they make similar mistakes with other events that do not bubble or is it just 'load' that is special?
Core QA
Opera Software ASA

21. December 2006, 05:47:18

pappu5star

Posts: 1

I want to capture the URLs browsed by Opera browsers. For this i can use the events Document.Load and get the URL. But my concern is "How to attach that HTML file to Opera browser .. so that my file code gets executed with the Opera browser automatically. Opera does not facilitates Add-On... so what can be other solution?

Pappu

22. February 2007, 01:08:35

mdajobs

Posts: 1

I appreciate your writing skill and thank you for your article.

It occured to me after studying it carefully and spending all day yesterday trying to get a code sample to work in Opera 9 and failing, that the most productive lead in you could have to help others get up to speed quickly using addEventListener, is a complete copy and paste example that one could run and view the code to see how it works. The way it is now, you have partial code and a great article and I'm still scratching my head trying to figure out why it doesn't work for me.

Here is the sample I fashioned around your code snippet. When I run it, I never see the confirm dialog, only the inline alerts. Please advise.

<html>

<head>

<script>

document.body.addEventListener( 'click', function(e){ if(confirm('Stop event?'))e.stopPropagation(); }, false );

</script>

</head>

<body>

<h1 onclick='alert("<h1> onclick!")'>Click to test.</h1>

<p onclick='alert("<p> onclick!")'>Click to test.</p>

</body>

</html>


24. February 2007, 01:12:57

tenno-seremel

Shinigami mentat

Posts: 316

2 mdajobs:
If you move document.body.addEventListener() into the body element in will work. Otherwise it says "Could not convert undefined or null to object".
=> WAS KI RA WAS YEA ERRA GYUSS VONN MELENAS TES AR YORR
EXEC hymme 2X1/0>>000111001111

Forums » Dev.Opera » Archived Article Discussions