I see 2 main ways to set events in JavaScript:
Add an event directly inside the tag like this:
<a href="" onclick="doFoo()">do foo</a>
Set them by JavaScript like this:
<a id="bar" href="">do bar</a>
and add an event in a <script>
section inside the <head>
section 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!