two different behaviours with 'return value' of a function

D

Dave

Hi,

I tried something with 'return value' of a function and i got two different
behaviours.
My question is: why does method 1 not work?
Thanks
Dave

method 1: here, whatever i choose (ok or cancel), i go to 'webpage.htm'
<body>
<a id="my" onClick="retvalue()" href="webpage.htm" >click here</a>

<script type="text/javascript">
function retvalue()
{
if (confirm("go to link?") == false)
return false
}
</script>
</body>



method 2: here, when clicking on 'cancel', i don't go to 'webpage.htm'
<body>
<a id="my" href="webpage.htm" >click here</a>

<script type="text/javascript">
function retvalue()
{
if (confirm("go to link?") == false)
return false
}
document.getElementById("my").onclick=retvalue
</script>
</body>
 
S

Sean Jorden

Dave said:
Hi,

I tried something with 'return value' of a function and i got two
different behaviours.
My question is: why does method 1 not work?
<a id="my" onClick="retvalue()" href="webpage.htm" >click here</a>

you need to do this:

onClick="return retvalue()"


I don't know why method 2 works.. perhaps this intrinsically treats all
functions as returning values.
 
L

Laurent Bugnion, GalaSoft

Hi,
Hi,

I tried something with 'return value' of a function and i got two different
behaviours.
My question is: why does method 1 not work?
Thanks
Dave

method 1: here, whatever i choose (ok or cancel), i go to 'webpage.htm'
<body>
<a id="my" onClick="retvalue()" href="webpage.htm" >click here</a>

Should be onClick="return retvalue();"

Laurent
 
R

Richard Cornford

Dave said:
I tried something with 'return value' of a function and i got
two different behaviours.
My question is: why does method 1 not work?
<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.
 
L

Lasse Reichstein Nielsen

Sean Jorden said:
I don't know why method 2 works.. perhaps this intrinsically treats all
functions as returning values.

A call to a function that doesn't perform a return statement (or one
that has a return statement with no expression after) evaluates to
"undefined". In this case, the function returns false when it needs
to, and undefined otherwise.

There is no need to compare to false and then return false.
The shorter version is
function retvalue() {
return confirm("Follow link?");
}

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top