"Dave" <no@dsfg.vb> wrote in message
news:bj**********@reader11.wxs.nl...
I tried something with 'return value' of a function and i got
two different behaviours.
My question is: why does method 1 not work?
<snip> <a id="my" onClick="retvalue()"
href="webpage.htm" >click here</a>
<snip>
The string provided as the value in an event handling attribute is taken
by the browser and used as the function body for a function that it
creates as the event handler. Your attribute string produces the
equivalent of:-
document.getElementById("my").onclick = function(event){
retvalue();
};
- and that event handling function has no return value so it will not
influence the default behaviour of the link. If you used -
onclcik="return retvalue();" - you would get the expected effect.
<script type="text/javascript">
function retvalue()
{
if (confirm("go to link?") == false)
return false
}
<snip>
The - confirm - function returns a boolean value, the comparison
operator produces a boolean value and the - if - statement requires its
expression to result in a boolean value (else it will type-convert it to
boolean). You never need to compare a boolean value with a boolean value
within an - if - statement. As a result:-
if (confirm("go to link?") == false)
-is equivalent to:-
if (!confirm("go to link?"))
- and the whole function can be simplified to:-
function retvalue(){
return confirm("go to link?");
}
- just passing the boolean value returned by the - confirm - function on
as the return value for the - retvalue - function.
Richard.