@Kor: With all due respect (and I'm not doing this to be argumentative, please don't take it as such), that is not correct.
Yes, these two versions indeed act differently because they are coded differently as inline event handlers.
Code:
<a href="http:www.google.com" onclick="preventLinkAction()">click</a>
<a href="http:www.google.com" onclick="return preventLinkAction()">click</a>
The function called in the first version may as well not return anything at all since that return value is never used.
Quote:
Originally Posted by Kor
No. You must ask also for a return within the handler.
|
Exactly. When the handler is attached from within a script (not inline as above), the return from within the handler executes whether it is assigned by reference or executed within an anonymous wrapper.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Prevent Link</title>
<script type="text/javascript">
function preventLinkAction(){
return confirm("Follow link?");
}
window.onload = function(){
document.getElementById("linkA").onclick = function(){return preventLinkAction();};
document.getElementById("linkB").onclick = preventLinkAction;
}
</script>
</head>
<body>
<a href="http://google.com/" id="linkA">link A</a>
<a href="http://google.com/" id="linkB">link B</a>
</body>
</html>
The two links in the demo above behave identically.
Since there is no problem interpreting the return from the handler, is there a specific reason to wrap the handler in an anonymous function? Again, I'm not asking to be argumentative, I'm wondering if there is an advanced issue (closure, etc.) that I'm missing here.