473,748 Members | 7,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="retval ue()" href="webpage.h tm" >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.h tm" >click here</a>

<script type="text/javascript">
function retvalue()
{
if (confirm("go to link?") == false)
return false
}
document.getEle mentById("my"). onclick=retvalu e
</script>
</body>
Jul 20 '05 #1
4 16295
"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="retval ue()" href="webpage.h tm" >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="retval ue()" href="webpage.h tm" >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="retval ue()"
href="webpage.h tm" >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.getEle mentById("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
8696
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 class: public abstract class Function { public abstract Number evaluate(Number parameter); } and i have a descendant of that class:
4
1825
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 suppose apple will have different price for different objects. i have a class food such that: food.showname(); //return string apple food.showname(); // return string orange
14
23432
by: digital | last post by:
hello anyone... pls explain me , how different between function and procedure for C/C++ and Pascal. Thankx......
6
1658
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 operators '^', '&', and '|' have lower precedance than '==', '!=', '>=", etc. I discovered this when the statement "if (array1 ^ array2 == 0xff)" failed to do what I expected. To me, it doesn't make any sense to give the bitwise operators lower...
10
2867
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 by return types")? Public Function test() As Integer Return 1 End Function Public Function test() As Single
21
3383
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 USA is mm/dd/yyyy and mexico is dd/mm/yyyy. So, with the computer set to mexico, any standard CDATE function is going to return the date in the dd/mm/yyyy setting since that is what the computer is set to. I want to be able to enter a date in...
2
1266
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 gives an error at line: "dtreader = comd.ExecuteReader" (the second) "There is already an open DataReader associated with this Command which must be closed first."
1
2055
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 both D1 and D2 have the function "doit". But there return type should be different. Is it possible? Thanks,
2
1503
by: rathour | last post by:
<?php session_start(); require_once('db.php'); include('functions.php'); $user = get_username ( $_SESSION ); //if ( $_SESSION ): checkLogin ( '1 2' ); ?>
0
8989
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8828
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9537
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8241
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.