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
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.