Connecting Tech Pros Worldwide Help | Site Map

two different behaviours with 'return value' of a function

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 20th, 2005, 10:15 AM
Dave
Guest
 
Posts: n/a
Default two different behaviours with 'return value' of a function

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>



  #2  
Old July 20th, 2005, 10:15 AM
Sean Jorden
Guest
 
Posts: n/a
Default 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.
  #3  
Old July 20th, 2005, 10:15 AM
Laurent Bugnion, GalaSoft
Guest
 
Posts: n/a
Default 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

  #4  
Old July 20th, 2005, 10:15 AM
Richard Cornford
Guest
 
Posts: n/a
Default 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.


  #5  
Old July 20th, 2005, 10:15 AM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
Default 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.'
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.