The difference between the first and the second, is that the second passes along the return value from the function and makes it the return value of the event handler. That makes it possible for the function to return false
to stop the default action of the event. For a button that doesn't make a difference, as there is no default action, but if it was a link it would be useful. Example:
function handleClick() {
// do something here
return false;
}
<a href="fallback.html" onclick="return handleClick();">click me</a>
The script will keep the link from navigating to fallback.html
.
The third code is incorrect in two ways. As it stands, it causes a syntax error. The example should probably be:
<button onclick="javascript:a()">
Sometimes people put javascript:
(not javascript
) first in an event handler out of habit, or because they think that it should be there. It comes from using Javascript in an URL in a link:
<a href="javascript:alert('hello')">click me</a>
When used like that, it tells the browser that it's a piece of script to run, not an URL to navigate to. If you use it incorrectly in an event handler, it will instead be interpreted as a label. It won't stop the script from working, because it happens to be valid syntax, but it's useless.