By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,558 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,558 IT Pros & Developers. It's quick & easy.

How to discover if attachEvent was done to an element

P: n/a
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

Aug 28 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
On Aug 28, 4:37 pm, Max <krya...@yahoo.comwrote:
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

Aug 29 '07 #2

P: n/a
Max wrote:
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
--
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, <f8*******************@news.demon.co.uk>
Aug 29 '07 #3

P: n/a
David Golightly wrote:
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
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Aug 29 '07 #4

P: n/a
On Aug 28, 6:18 pm, Thomas 'PointedEars' Lahn wrote:
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.

Aug 29 '07 #5

P: n/a
David Golightly wrote:
On Aug 28, 6:18 pm, Thomas 'PointedEars' Lahn wrote:
>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.
Which assertion exactly?
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Aug 29 '07 #6

P: n/a
On Aug 28, 6:26 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
David Golightly wrote:
On Aug 28, 6:18 pm, Thomas 'PointedEars' Lahn wrote:
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.

Which assertion exactly?
PointedEars.ASSERT(YUI == 'junk code');

Aug 29 '07 #7

P: n/a
Max
On Aug 28, 6:05 pm, David Golightly <davig...@gmail.comwrote:
On Aug 28, 4:37 pm, Max <krya...@yahoo.comwrote:
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
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

Aug 29 '07 #8

P: n/a
Max
On Aug 28, 6:16 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Max wrote:
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
--
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, <f806at$ail$1$8300d...@news.demon.co.uk>
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

Aug 29 '07 #9

P: n/a
On Aug 28, 9:43 pm, Max <krya...@yahoo.comwrote:
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

Aug 29 '07 #10

P: n/a
Max
On Aug 29, 11:48 am, David Golightly <davig...@gmail.comwrote:
On Aug 28, 9:43 pm, Max <krya...@yahoo.comwrote:
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/lib...703974.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

Aug 29 '07 #11

P: n/a
dh**********@gmail.com said the following on 8/29/2007 10:54 PM:

<snip>
BTW - How to add a signature in my message footer? I'm using the
Google groups UI.
Unless they have changed something in the last few weeks to allow it,
you can't other than to paste/type it in manually each time.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 30 '07 #12

P: n/a
In comp.lang.javascript message <46**************@PointedEars.de>, Wed,
29 Aug 2007 03:16:56, Thomas 'PointedEars' Lahn <Po*********@web.de>
posted:
>Max wrote:
>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.)
And YSCIB, Eig-Ears.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)
Aug 30 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.