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

detect listeners

P: n/a
Hello,

Is there a way (in JS) to detect listeners on an element and identify
them? The purpose would be to selectively attach additional listeners
if certain conditions are met. For example, I want to attach
listenerA to links that go to PDFs and listenerB to links that go off
site. But, I don't want both A and B attached to offsite links that
point to PDFs.

I would like to be able to choose which to attach, so that
if (! ele has listenerA) attach listenerB. Or vice versa.

I have looked through my reference materials and googled but I don't
see anything that seems to answer this question.

Thanks.

mp
--
Michael Powe mi*****@trollope.org Naugatuck CT USA
"...we built a new continent in cyberspace where we could go live and be
free. And the nice thing is that, because we built it, we didn't have
to steal it from aboriginal peoples. It was completely empty, and we
invite everyone to live there with us. No immigration restrictions.
There's room for everyone in the world. Come live in the free world
and be free. That's our idea." -- Richard Stallman
Jan 28 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 2006-01-28, Michael Powe <mi**********@trollope.org> wrote:
Hello,

Is there a way (in JS) to detect listeners on an element and identify
them? The purpose would be to selectively attach additional listeners
if certain conditions are met. For example, I want to attach
listenerA to links that go to PDFs and listenerB to links that go off
site. But, I don't want both A and B attached to offsite links that
point to PDFs.

I would like to be able to choose which to attach, so that
if (! ele has listenerA) attach listenerB. Or vice versa.

I have looked through my reference materials and googled but I don't
see anything that seems to answer this question.


listeners attached using HTML are anonymous functions in mozilla you can use
the .toSource method of the appropriate domObject.onevent to find their
content. myDiv.onclick.tosource()

Listners attached using domObject.onEvent=functionName;
are functions can be identified using ==

if( myDiv.onclick == functionName ) ...

Bye.
Jasen
Jan 29 '06 #2

P: n/a
Michael Powe wrote:

Is there a way (in JS) to detect listeners on an element and identify
them?


For those listeners added as attributes in the markup (e.g.
<input onclick="alert(event.type);" type="button" value="test">
) the element object has a property named oneventname (e.g.
inputObject.onclick
) which is a function object (e.g.
if (typeof inputObject.onclick == 'function')
)
For listeners added with W3C DOM Level 2 addEventListener there is no
way to detect those listeners, you are supposed to keep track of that
yourself in your code.

DOM Level 3 adds a method hasEventListenerNS
<http://www.w3.org/TR/DOM-Level-3-Events/events.html#Events3-hasEventListenerNS>
but that is not a W3C recommendation, only a note. I don't know any
browser implementing that.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jan 29 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.