2011
01.09

Necessity is the mother of invention, and 9 times out of 10, when it comes to web development, someone has most likely had the same problem you are having. Thank goodness that you are working in the best environment to find the answer to your problem, the “interwebs”.

The other day I needed to solve a simple problem. Find out if a particular javascript handler was already bound to the document, so that I could bind it if need be, and not bind it multiple times if it was already bound (I would be binding and unbinding frequently based on how my app was working). To my surprise, javascript has no built in way to check to see if a particular event is already bound. There were some workarounds I found, and finally settled on adding a namespace to my particular event and then unbinding and then re-binding the event every time to ensure that it would only have the handler executed once. It worked, but I knew that this couldn’t be the best solution.

I found this post on stack overflow which details how to loop through listeners on a particular element. I tried this, but couldn’t get any good details out of the events. I couldn’t distinguish between the exact event I wanted to work with, and other events that where already bound to the element that I would not want to effect. The second answer to the question pointed to a jQuery plugin called hasEventListener written by sebastienp. This plugin provides multiple ways to check different elements for events, including namespaced events. Sweet!!

The only problem was that sebastien has written it to work with jQuery 1.4.3+, and I was running 1.4.2. I probably could have tired to upgrade, but I am currently using jQuery tools heavily in my app, and it has not be updated to work with jQuery 1.4.3 yet (at least their download page shows that it is not). I tried using the plugin anyway, and it blew up on me.

I have never written a jQuery plugin of my own (I obviously should), and only fixed trivial things in other plugins. When I looked at this plugin, there was nothing that stood out to me as to what was causing the issue. So I took a shot in the dark and sent sebastien a message via twitter asking if there was any way to get it working with an earlier version on jQuery. After waiting a little while, I reverted back to the old unbind/bind trick and went to bed. I don’t expect my issues to become someone else’s.

To my surprise, when I woke up the next morning, not only had sebastien responded but he had updated the plugin to work with jQuery 1.2.3+ (not sure if someone else needed it for an earlier version, or he just checked it on earlier versions). He lives in France and said he was sleeping when I first messaged him. I was floored. After a few more correspondence via twitter I got the plugin working correctly and I could know that my app was going to be working much more efficiently. Instead of always unbind and binding the handler, I just check to see if it’s there, and only bind if I need to.

I know I title this post “collaboration”, but I didn’t really do anything but notify sebatien that I needed help, but I wanted to stress his end of collaborating to help me solve my problem. I hope to return the favor to him or someone else someday soon.

Thanks Again

https://github.com/sebastien-p/jquery.hasEventListener

  • http://topsy.com/sullerton.com/2011/01/jquery-haseventlistener-and-developer-collaboration/?utm_source=pingback&utm_campaign=L2 Tweets that mention Sullerton » jQuery hasEventListener and developer collaboration — Topsy.com

    [...] This post was mentioned on Twitter by mattotodd and Sebastien P.. Sebastien P. said: Thank you, that's awesome ! // RT @mattotodd : @_sebastienp thanks again for the help. My Thanks http://bit.ly/hCnK7V [...]

  • sainath

    great!your article is very good impressed me