# Tuesday, October 11, 2005

.js != .cs

And I think Mak wants them to be more similar. One item in particular that c# has is the ability to have multiple events fire off of a single event through delegates. While javascript will never be c#, recent additions to javascript get it a bit closer with addEventListener. Unfortunately we live in a world where people aren't all using cutting edge browsers, and it will take time for that language improvement to be supported by all. In the mean time? Borrow good hack techniques like this one from Simon Willison (via Joe Grossberg).

For a script ... to be properly reusable, it needs to behave nicely in the presence of other scripts. This means that assigning a callback function directly to the window.onload handler is out of the question as doing so will over-ride previously assigned callbacks from other scripts. The correct way of adding a handler to an event without over-riding other handlers is to use modern event attachment method, which sadly differ between IE/Windows and other browsers. Scott Andrew's addEvent function handles the differences for you but comes with one major and rarely discussed drawback: it fails silently in IE5/Mac. If you care about the many Mac users still on OS9, you need to support that browser.

[ Simon Willison: Executing JavaScript on page load ]

I like this code snippet. I've used this technique before, but never as a generic function that could be reused. With a bit of tweaking this could easily be extended to handle other events (though the window's onload is usually the only one that everyone's fighting for) or allow the ability to specify if the added event comes before or after all current events (just in case you need to jack something in as the #1 thing to do).

#    Comments [2] |
Tuesday, October 11, 2005 10:14:38 AM (Eastern Standard Time, UTC-05:00)
My only complaint with it was that I couldn't, for the life of me, find out how it's licensed (I even sent an email asking him).

So I went ahead and used it on a page for work, but that's still annoying.

Anyhow, his blog is a must-read for anyone who works with JS.
Tuesday, October 11, 2005 10:52:11 AM (Eastern Standard Time, UTC-05:00)
No mention of copyright on his site, and no mention of copyright restrictions or freedoms on the SitePoint site (where he duplicated the code).

I would expect, under US law, that means no one can use it. But I'd also expect that he didn't even think of that because it's a tiny piece of javascript code for his article.

Oddly, I bet if you duplicated a portion of the article and didn't give proper citing, that he would likely be peeved.
Comments are closed.