How to discover if attachEvent was done to an element
|
|
Thread rating:  |
Max - 29 Aug 2007 00:37 GMT 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
David Golightly - 29 Aug 2007 02:05 GMT > Hi All, > [quoted text clipped - 5 lines] > 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
Thomas 'PointedEars' Lahn - 29 Aug 2007 02:18 GMT > 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 [quoted text clipped - 3 lines] > 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
 Signature var bugRiddenCrashPronePieceOfJunk = ( navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1 ) // Plone, register_function.js:16
David Golightly - 29 Aug 2007 02:24 GMT > 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.
Thomas 'PointedEars' Lahn - 29 Aug 2007 02:26 GMT >> 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
 Signature var bugRiddenCrashPronePieceOfJunk = ( navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1 ) // Plone, register_function.js:16
David Golightly - 29 Aug 2007 02:30 GMT On Aug 28, 6:26 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de> wrote:
> >> Mere handling of previously registered events is hardly a > >> justification for using junk code. [quoted text clipped - 3 lines] > > Which assertion exactly? PointedEars.ASSERT(YUI == 'junk code');
Max - 29 Aug 2007 05:43 GMT > > Hi All, > [quoted text clipped - 19 lines] > > -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
David Golightly - 29 Aug 2007 19:48 GMT > 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 [quoted text clipped - 6 lines] > > 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
Max - 29 Aug 2007 23:07 GMT > > 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 [quoted text clipped - 32 lines] > > -David David,
Thanks for your replies. I'll post solution for this pesky problem if I find any.
Max
dhtmlkitchen@gmail.com - 30 Aug 2007 03:54 GMT > > > 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 [quoted text clipped - 39 lines] > > 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
Randy Webb - 30 Aug 2007 04:16 GMT dhtmlkitchen@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.
 Signature Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Thomas 'PointedEars' Lahn - 29 Aug 2007 02:16 GMT > 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
 Signature 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$8300dec7@news.demon.co.uk>
Thomas 'PointedEars' Lahn - 29 Aug 2007 02:24 GMT >> I need to check if attachEvent was done to an element. The problem is >> that attachEvent does not save this information anywhere. Is there [quoted text clipped - 4 lines] > 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
 Signature Anyone who slaps a 'this page is best viewed with Browser X' label on a Web page appears to be yearning for the bad old days, before the Web, when you had very little chance of reading a document written on another computer, another word processor, or another network. -- Tim Berners-Lee
Max - 29 Aug 2007 05:49 GMT On Aug 28, 6:16 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de> 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 [quoted text clipped - 18 lines] > 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
Dr J R Stockton - 30 Aug 2007 22:24 GMT In comp.lang.javascript message <46D4C908.6050608@PointedEars.de>, Wed, 29 Aug 2007 03:16:56, Thomas 'PointedEars' Lahn <PointedEars@web.de> posted:
>> 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.
 Signature (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)
|
|
|