I was watching my del.icio.us/popular links today and saw “JavaScript.com” scroll by. Amused, I decided to take a look. I remember looking at sites like this back in ’98-’99 when I wanted to do “cool” effects, just copy-and-pasting snippets into a page and hoping that it would work.
So I started to look through the snippets on their web site. I knew that some bad JavaScript code was still written – but dear god – there’s some awful code on their site. I can’t even dignify it with a link, for fear that some PageRank may bleed their way.
I then stumbled across a so-called “Add/Remove an Event” snippet. Intrigued, I decided to take a look. You may remember that I won the Quirksmode addEvent contest last year with this entry. Honestly, it’s pretty rough – I’ve moved on and use other techniques these days.
Now, I’d like you to take a look at my code and compare it to the code that was presented on JavaScript.com:
My Original addEvent/removeEvent Code
function addEvent( obj, type, fn ) { if ( obj.attachEvent ) { obj['e'+type+fn] = fn; obj[type+fn] = function(){obj['e'+type+fn]( window.event );} obj.attachEvent( 'on'+type, obj[type+fn] ); } else obj.addEventListener( type, fn, false ); } function removeEvent( obj, type, fn ) { if ( obj.detachEvent ) { obj.detachEvent( 'on'+type, obj[type+fn] ); obj[type+fn] = null; } else obj.removeEventListener( type, fn, false ); }
The “New” addEvent/removeEvent snippet from JavaScript.com
//------------------------------------ // addEvent function addEvent(obj,type,fn){ try{ if(obj.addEventListener) obj.addEventListener(type,fn,false); else if(obj.attachEvent){ obj["e"+type+fn]=fn obj[type+fn]=function(){ obj["e"+type+fn](window.event)} obj.attachEvent("on"+type,obj[type+fn]); } } catch(e){/*alert(e)*/} } //------------------------------------ // removeEvent function removeEvent(obj,type,fn){ if(obj.removeEventListener) obj.removeEventListener(type,fn,false); else if(obj.detachEvent){ obj.detachEvent("on"+type,obj[type+fn]) obj[type+fn]=null obj["e"+type+fn]=null; } }
There’s a couple points about this snippet that I find particularly amusing:
- It’s presented under the copyright of some other blogger: askApache.com (although, this seems to be more of a case of mistaken identity than anything else).
- There’s an extra try/catch block that silently garbles up any errors. What errors? You’re binding an event!
- There’s an extra check for IE (obj.detachEvent) but doesn’t throw any error when the event isn’t able to be bound.
In all, I’d hate to be the pour soul trying to debug my first JavaScript programming and not knowing why I wasn’t getting any relevant errors. We should get Sun to start flexing their JavaScript trademark muscle and take down sketchy sites like this, passing them over to non-profit organizations who could actually move JavaScript out of the dark ages.
Update: The askApache webmaster has replied in the comments – it was a misunderstanding and the relevant copyright info is being put in place. Yay! That being said, JavaScript.com still has really poor-quality code. It’s like we need a reset switch for the world of JavaScript knowledge – delete all the old, bad, code and stick with the current, high quality, code and libraries.
askApache Webmaster (January 22, 2007 at 1:14 am)
John, this is the askApache webmaster and I want to offer you my full apologies for not giving you credit where you definately deserve to be credited.
I have just modified the post at My favorite Javascript 2007 to reflect the updated version based on your post here. I also placed a couple prominent links to your site and page and make clear that the code is based on your winning quirksmode entry.
I also contacted the admins who are in charge of the post at Add/Remove an Event with updated code including a paragraph giving you credit and they should update their site soon.
Sorry for this whole thing, I love and use your code all the time and I didn’t mean to be irresponsible in posting it. No hard feelings I hope.
John Resig (January 22, 2007 at 1:41 am)
@askApache: No hard feelings at all! Thanks for taking care of this; you were surprisingly fast. I guess I was just venting at the quality of code on JavaScript.com and got a little bit too excited. I’ve updated the post to reflect your comment, and to tone things down a bit. Sorry for the trouble.
askApache (January 31, 2007 at 12:33 pm)
Cool.. I’m new to blogging so its a learning experience for me :)
I am really curious if you have come up with any improvements to any of the functions.. They work on every browser i’ve tested on, I just want to learn more about it since this is such a crucial function for all the javascript code that I write.