Connecting Tech Pros Worldwide Forums | Help | Site Map

two different behaviours with 'return value' of a function

Dave
Guest
 
Posts: n/a
#1: Jul 20 '05
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>



Sean Jorden
Guest
 
Posts: n/a
#2: Jul 20 '05

re: two different behaviours with 'return value' of a function


"Dave" <no@dsfg.vb> wrote in news:bj9kno$m5s$1@reader11.wxs.nl:
[color=blue]
> 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?[/color]
[color=blue]
> <a id="my" onClick="retvalue()" href="webpage.htm" >click here</a>[/color]

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.
Laurent Bugnion, GalaSoft
Guest
 
Posts: n/a
#3: Jul 20 '05

re: two different behaviours with 'return value' of a function


Hi,

Dave wrote:
[color=blue]
> 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>[/color]

Should be onClick="return retvalue();"

Laurent
--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Richard Cornford
Guest
 
Posts: n/a
#4: Jul 20 '05

re: two different behaviours with 'return value' of a function


"Dave" <no@dsfg.vb> wrote in message
news:bj9kno$m5s$1@reader11.wxs.nl...[color=blue]
>I tried something with 'return value' of a function and i got
>two different behaviours.
>My question is: why does method 1 not work?[/color]
<snip>[color=blue]
> <a id="my" onClick="retvalue()"
> href="webpage.htm" >click here</a>[/color]
<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.
[color=blue]
> <script type="text/javascript">
> function retvalue()
> {
> if (confirm("go to link?") == false)
> return false
> }[/color]
<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.


Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#5: Jul 20 '05

re: two different behaviours with 'return value' of a function


Sean Jorden <s_j_o_r_d.e.n@no.spam.n_o_r_a.d.a.com> writes:
[color=blue]
> I don't know why method 2 works.. perhaps this intrinsically treats all
> functions as returning values.[/color]

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
--
Lasse Reichstein Nielsen - lrn@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Closed Thread