Networking | Hardware | Software | Multimedia | System | Unix&Linux | MBA

Home>>Hardware>>when to use return functionName on events

when to use return functionName on events

Vijay Venkat
03-16-2006, 03:01 PM
Hi
I am confused with the usage of javascript of the form
<a href=http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/"www.google.com" onclick="return someFunction(this)"/>

and

<a href=http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/"www.google.com" onclick="someFunction(this)"/>

What is the difference

Here is a piece of code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<script>
firstClick = true;
function clickFn(field)
{

if (firstClick == true)
{
firstClick = false;;
return true;
}
else
{
alert('google');
field.href=http://codingforums.com/archive/index.php/"http://www.google.com";
return false;
}
}
</script>
</HEAD>

<BODY>

<!-- HERE -->
<a href=http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/"http://www.webmonkey.com" onclick="return clickFn(this);">Google</a>

</BODY>
</HTML>


See this piece in the code:
<!-- HERE -->
<a href=http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/"http://www.webmonkey.com" onclick="return clickFn(this);">Google</a>

The behaviour changes when the above is
<a href=http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/"http://www.webmonkey.com" onclick="clickFn(this);">Google</a>
Note it does not have a return in front of it.

I am trying to disable the click on the link twice. So the user gets to see his first response for his first click while we disable the second one. Any pointers will be very helpful.


Thanks,
Vijay

Kor
03-16-2006, 03:12 PM
You will see better the usage of return false with this
<a href=http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/"www.google.com" onclick="return false">adsafd</a>

return false will stop the HTML action (href, submit...)

Beagle
03-16-2006, 03:52 PM
Setting an onclick looks like this:

<element onclick="functionbody;" />

element.onclick = function() {
functionbody;
};

There's a big difference between the following 2 functions:

function x()
{
alert(document.location);
}

function y()
{
return document.location;
}

As Kor said, the only reason to return a value in the body of an event handler is to suppress/enable the default action of that element. For links, if you click a link, the default action is to navigate to the destination. If you return true from your event handler, the location changes, if you return false, the default action is supressed, and the location does not change.

Vijay Venkat
03-17-2006, 11:59 AM
Kor / Beagle

Thanks for the reply. I am aware/understand that if i return false, the appropriate navigation does not take place.

I am not able to understand this piece and how it works.

<!-- HERE -->
USAGE 1
<a href=http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/"http://www.webmonkey.com" onclick="return clickFn(this);">Google</a>

USAGE 2
The behaviour changes when the above is
<a href=http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/http://codingforums.com/archive/index.php/"http://www.webmonkey.com" onclick="clickFn(this);">Google</a>

Note that the function clickFn(xy) returns a boolean value. I am not clear whether we need to add a explicit return as in USAGE 1

Which is correct USAGE 1 or USAGE 2?

In the html i posted if comment out one usage and use the other and click on the link very fast twice you can see the effect. Sorry if it is silly. I am not really getting it.

Note: Don't be offeneded by the text in bold. I just want to stress what i am not able to understand.

Kind Regards,
Vijay

Kor
03-17-2006, 12:07 PM
the returned values of the function are Boolean values true/false. Now let's take the false variant.

onclick="return clickFn(this);" is the same thing with onclick="return false;" so that the "href" action is blocked.

onclick="clickFn(this);" is the same with onclick="false;" which is not the same thing. In fact means and does nothing, so that the "href" action will be permitted.

So that USAGE 1 is the correct syntax.


 

TOP

Hyperthreading hurts s
HP sharpens blade PC l
Apple goes for Quad po
Itanium gets scaled do

For more info

HP sharpens blade PC l
Hyperthreading hurts s
can not call click() i
script does not work o
smart menu 
write to an html file 
FireFox doesn't recogn
Changing e.keyCode (e.
Clearing a range creat
UPDATE: My mouse over 

News Archive

onChange Help 
beforeload? vs onload.
Problem with document.
decreasing my javascri
Problem with \\ and \ 
how to stop scrolling?
Change Label Text Colo
Can I forward a page b
2 javascript functions
Validating a textbox 

Related stories:

Hiding an email address using javascript
Dropdown works in IE / Firefox but not Safari
Styling disabled form elements?
Dynamically Populate Slideshow
Parameter Used to Update Formula
script that adds item to cart without going to shopping cart
problem with HTTPS
ebaY Category Picker doesn't work
window.opener problem when navigating away

Copyright@2004-2005 www.zzcoke.com All Right Reserved

advanced web statistics