It is considered best practice to use so-called unobtrusive javascript . This means that you separate the layout of your HTML from the behavior of the elements. Therefore, instead of using the onclick attribute, which mixes the structure of elements and behavior, you place your DOM structure in the markup, and then attach event handlers through javascript.
This means that it is considered best practice to use javascript for attached event handlers as follows:
<html>
<script>
... bind event handlers to your DOM and set behaviours here
</script>
<body>
.... layout your DOM here
</body>
<html>
This has advantages for code longevity and extensibility. This approach works very well with javascript libraries like jQuery.
From a performance point of view, you can achieve better performance through an unobtrusive approach to using JavaScript with an intelligent caching strategy.
javascript .