How to discover if attachEvent was done to an element

M

Max

Hi All,

I need to check if attachEvent was done to an element. The problem is
that attachEvent does not save this information anywhere. Is there
any
way to do this???


Thanks,
Max
 
D

David Golightly

Hi All,

I need to check if attachEvent was done to an element. The problem is
that attachEvent does not save this information anywhere. Is there
any
way to do this???

Thanks,
Max

Do you need to be notified exactly *when* attachEvent was used? Or do
you come along later and inspect the element to see if it had had an
event listener attached at some point in the past?

Also, for this an other reasons, I don't particularly care to use IE's
proprietary attachEvent at all. Though some would eviscerate me for
saying this, try using the YAHOO! UI library (http://
developer.yahoo.com/yui/) to wrap the browser differences. Your life
will be MUCH easier: YAHOO exposes YAHOO.util.Event.getListeners, a
method that returns all (YAHOO-library) attached listeners on an
element.

-David
 
T

Thomas 'PointedEars' Lahn

Max said:
I need to check if attachEvent was done to an element. The problem is
that attachEvent does not save this information anywhere. Is there
any way to do this???

(Your Question Mark key is borken.)

I don't know any. But, since attachEvent() is IE-proprietary, you could
equally use the `onclick' property, which value you can test against.
Your code would still be proprietary, but would at least support more DOMs
(including NN4, Geckos, Opera, and presumably KHTML and WebKit). I wonder
why you think you need that, though.

Does anyone know a way to determine whether a DOM object has an event
listener added to it with Element::addEventListener(), or if it is possible
to return a list of the added listeners, and if yes, how?


PointedEars
 
T

Thomas 'PointedEars' Lahn

David said:
Also, for this an other reasons, I don't particularly care to use IE's
proprietary attachEvent at all. Though some would eviscerate me for
saying this, try using the YAHOO! UI library (http://
developer.yahoo.com/yui/) to wrap the browser differences. Your life
will be MUCH easier: YAHOO exposes YAHOO.util.Event.getListeners, a
method that returns all (YAHOO-library) attached listeners on an
element.

Mere handling of previously registered events is hardly a justification
for using junk code.


PointedEars
 
D

David Golightly

Mere handling of previously registered events is hardly a justification
for using junk code.

You'll have to back up this assertion with something other than
vitriol.
 
T

Thomas 'PointedEars' Lahn

Thomas said:
(Your Question Mark key is borken.)

I don't know any. But, since attachEvent() is IE-proprietary, you could
equally use the `onclick' property,

I meant the corresponding event handler properties:

foo.onclick = bar;

instead of

foo.attachEvent("onclick", bar)

etc.
which value you can test against.
Your code would still be proprietary, but would at least support more DOMs
(including NN4, Geckos, Opera, and presumably KHTML and WebKit). I wonder

Remove "including", change "WebKit" to "WebCore".
why you think you need that, though.
[...]


PointedEars
 
M

Max

Do you need to be notified exactly *when* attachEvent was used? Or do
you come along later and inspect the element to see if it had had an
event listener attached at some point in the past?

Also, for this an other reasons, I don't particularly care to use IE's
proprietary attachEvent at all. Though some would eviscerate me for
saying this, try using the YAHOO! UI library (http://
developer.yahoo.com/yui/) to wrap the browser differences. Your life
will be MUCH easier: YAHOO exposes YAHOO.util.Event.getListeners, a
method that returns all (YAHOO-library) attached listeners on an
element.

-David

I need to inspect the element and figure out whether it has attached
event handler or not. I'm writing an automation tool which basically
can load any URL and when page is loaded I want to inspect all
elements and see which events are attached. In case of onclick
property all I have to do just verify if element.onclick != null but
attachEvent does not affect the state of the property. I can not use
any libraries like Yahoo because I'm not the one who is writing a code
for the page.

Max
 
M

Max

(Your Question Mark key is borken.)

I don't know any. But, since attachEvent() is IE-proprietary, you could
equally use the `onclick' property, which value you can test against.
Your code would still be proprietary, but would at least support more DOMs
(including NN4, Geckos, Opera, and presumably KHTML and WebKit). I wonder
why you think you need that, though.

Does anyone know a way to determine whether a DOM object has an event
listener added to it with Element::addEventListener(), or if it is possible
to return a list of the added listeners, and if yes, how?

PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <[email protected]>

Thomas,

As I mentioned in my reply to David I'm not writing a code for the
page. I need to inspect elements of the existing page. So I have the
same question about attached event listeners.

Max
 
D

David Golightly

I need to inspect the element and figure out whether it has attached
event handler or not. I'm writing an automation tool which basically
can load any URL and when page is loaded I want to inspect all
elements and see which events are attached. In case of onclick
property all I have to do just verify if element.onclick != null but
attachEvent does not affect the state of the property. I can not use
any libraries like Yahoo because I'm not the one who is writing a code
for the page.

Max

Wow, I suspect there's just not going to be a solution to this
particular problem, which I rarely say, but in this case you may well
have stumped me and everyone on this list. (Yes, that's a
challenge.) For native object methods I'd recommend wrapping the
native code before the script loads and replacing it with a function
that logs each call to that function to your custom object that
records method usage. IE, however, is built defensively and won't let
you wrap COM object methods, and since attachEvent belongs to
IHTMLElement2 (which is part of the MSHTML and not the JSCRIPT
interface), this won't work. Try it. You'll get a nasty exception.

Listeners registered using attachEvent are maintained behind-the-
scenes, that is, they're kept hidden from the scope of the window
object and its children. So unless you have some way to inspect the
code running on the page for instances of attachEvent and you're able
to trace the element references used back to elements on the page,
which is clearly non-trivial, OR unless you have some way of embedding
the ShDocView component in a C# application and getting at the DOM
that way, you're probably not going to find a pure-JScript solution.

See: http://msdn2.microsoft.com/en-us/library/aa703974.aspx for more
info on attachEvent (and most everything else in the labyrinthine
annals of Internet Explorer's COM system).

-David
 
M

Max

Wow, I suspect there's just not going to be a solution to this
particular problem, which I rarely say, but in this case you may well
have stumped me and everyone on this list. (Yes, that's a
challenge.) For native object methods I'd recommend wrapping the
native code before the script loads and replacing it with a function
that logs each call to that function to your custom object that
records method usage. IE, however, is built defensively and won't let
you wrap COM object methods, and since attachEvent belongs to
IHTMLElement2 (which is part of the MSHTML and not the JSCRIPT
interface), this won't work. Try it. You'll get a nasty exception.

Listeners registered using attachEvent are maintained behind-the-
scenes, that is, they're kept hidden from the scope of the window
object and its children. So unless you have some way to inspect the
code running on the page for instances of attachEvent and you're able
to trace the element references used back to elements on the page,
which is clearly non-trivial, OR unless you have some way of embedding
the ShDocView component in a C# application and getting at the DOM
that way, you're probably not going to find a pure-JScript solution.

See:http://msdn2.microsoft.com/en-us/library/aa703974.aspxfor more
info on attachEvent (and most everything else in the labyrinthine
annals of Internet Explorer's COM system).

-David

David,

Thanks for your replies. I'll post solution for this pesky problem if
I find any.

Max
 
D

dhtmlkitchen

David,

Thanks for your replies. I'll post solution for this pesky problem if
I find any.

Max

Max,

David is right.

The only way you'll be able to get the associated listeners is to roll
your own registry. Yahoo Event system would work fine, too.

I actually think YUI events is a pretty good API to work with, but if
there are some problems with it, I'd love to know about them!

There are a few dislikes I have with it:
* the CustomEvent part where you have to pass silent=true and
signature = FLAT.
* addListener will fail silently when you pass a string that is an
ID of an element that does not exist in the document at any time.
* util, as name for a package, is lame. It should be in a package
called event. Yahoo.event.Event.
I prefer fast failure over silent failure, and so I don't use an ID
string for the first arg - just an object ref.

The CustomEvent is easy to use and makes it easy to create an
EventProvider out of your class.

Any detailed, specific, objective criticism for YUI event system?
(please no 'it sux' type of criticism - that doesn't help anyone and
won't convince anyone, either) Post up your analysis.

BTW - How to add a signature in my message footer? I'm using the
Google groups UI.

Thank you,

Garrett
 

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
474,158
Messages
2,570,882
Members
47,414
Latest member
djangoframe

Latest Threads

Top