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).
dasBlog theme modified from "mads simple" theme by Mads Kristensen
Disclaimer The opinions expressed herein are my own personal opinions.