I see mainly 2 ways to set events in JavaScript:
add event directly inside the tag like that:
<a href="" onclick="doFoo()">do foo</a>
Or
set them by JavaScript like that:
<a id="bar" href="">do bar</a>
And add event in a <script>
section inside <head>
or in an external JavaScript file, like that if you're using prototypeJS:
Event.observe(window, 'load', function() {
$('bar').observe('click', doBar);
}
I think the first method is easier to read and maintain (because the JavaScript action is directly bound to the link) but it's not so clean (because users can click on the link even if the page is not fully loaded, which may cause JavaScript errors in some cases).
The second method is cleaner (actions are added when the page is fully loaded) but it's more difficult to know that an action is linked to the tag.
Which method is the best?
A killer answer will be fully appreciated !