473,383 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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>
Jul 20 '05 #1
4 16267
"Dave" <no@dsfg.vb> wrote in news:bj**********@reader11.wxs.nl:
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.
Jul 20 '05 #2
Hi,

Dave wrote:
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
--
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

Jul 20 '05 #3
"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.
Jul 20 '05 #4
Sean Jorden <s_***********@no.spam.n_o_r_a.d.a.com> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Murat Tasan | last post by:
so i have a situation that i know cannot be solved directly, and i've already worked around it, but i was hoping to find an explanation as to why this behavior is as it is... i have an abstract...
4
by: news.hku.hk | last post by:
I would like to group different kinds of food. There are unknown number of kinds of food e.g. apple, orange, lemon, melon, ......... of course it's only a finite number but i don't know. also...
14
by: digital | last post by:
hello anyone... pls explain me , how different between function and procedure for C/C++ and Pascal. Thankx......
6
by: Rennie deGraaf | last post by:
In the last few days, I have discovered two "interesting" behaviours of the C language, both of which are apparently correct. Could someone please explain the reasoning behind them? 1. The...
10
by: Peter | last post by:
Hi, how can I do this (I don't really want to do this but what I do want the same function name but have different return types and the compiler keeps saying "public function .... they differ only...
21
by: Darin | last post by:
I have an applicatoin that works 100% perfect when running on a machine setup for English (United States), but when I change it to Spanish (Mexico), the dates start giving me fits. THe reason is...
2
by: Bart | last post by:
Hi, I ran the same code with two different providers (oledb abd sqlclient), and i got two different behaviours. The code with OLEDB runs perfect without error. The same code with SQLClient...
1
by: PengYu.UT | last post by:
Hi, Are there any walkaround to enable functions in the derived class with the same function name but different return type? In the following example, D1 and D2 are B's derived class. I want...
2
by: rathour | last post by:
<?php session_start(); require_once('db.php'); include('functions.php'); $user = get_username ( $_SESSION ); //if ( $_SESSION ): checkLogin ( '1 2' ); ?>
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.