Click to See Complete Forum and Search --> : use of return in href?


lisaliebe
02-09-2004, 09:41 AM
Hi

I have this javascript method that I call on a href and it returns either true or false. For the purpose of my program, I have the call embedded in a string using escape characters:

String eg = "<a href=\"javascript:myMethod('test')\">";

The javascript method is as follows:

function myMethod(value) {
c++;
if (c > 1) return false;
document.forms[0].var.value = value;
document.forms[0].submit();
}

I want to call a return on this so if the value is false, it does nothing, ie doesnt submit the form

href=\"javascript:return myMethod('test')\"

However when I do this, I get the javascript error

'return' statement outside of function

Can anyone shed any light on this?

Regards.

crh3675
02-09-2004, 09:53 AM
Will not work. you need to do this:

<a href="javascript:void()" onclick="return myMethod('test');">

lisaliebe
02-09-2004, 11:06 AM
That didnt seem to work for me here. Is there any other possible way? I already call another method on the OnClick=...is it possible to call more than one method from here?

crh3675
02-09-2004, 11:12 AM
You do not need to return false if your form.submit() only executes if certain requirements are met.

You cannot return multiple values in an OnClick event. Can you display the element code that you use to call the OnClick.

lisaliebe
02-09-2004, 11:39 AM
Unfortunely, if I do that the link wont work. This is a bit a problem

crh3675
02-09-2004, 11:43 AM
Can you display the element code that you use to call the OnClick.

crh3675
02-09-2004, 11:44 AM
Why don't you call the second function from within the first function?

lisaliebe
02-09-2004, 11:46 AM
forgot to add,

the code on the onClick is

function X() {
var yPos = window.event.clientY + document.body.scrollTop;
document.forms[0].val1.value=yPos;
}

If I dont handle the return value and its false, then I get a blank screen with the word 'false' on it.

lisaliebe
02-09-2004, 11:50 AM
In answer to you question,

"Why don't you call the second function from within the first function?"

I tried that and it didnt work. In the second method, I am capturing the y coord of the most recent mouse click and I lose focus on it when I call that method from the other method

crh3675
02-09-2004, 11:51 AM
What is calling that function?? Can you display the element that calls the function.

lisaliebe
02-09-2004, 11:57 AM
its a link as i said

String eg = "<a href=\"java script:myMethod('test')\" onClick=X()>";

myMethod() returns true/false and submits the form if true and shows a blank screen with the word 'false' if the method returns false

X() just sets a value on the form but needs to be called from this point as its calculating the y coordinate of the position the mouse clicked on the screen when it selected this href

ray326
02-09-2004, 07:49 PM
A better way to build the link is

<a href="#" onclick="myMethod('test');return false;">clickme</a>

crh3675
02-11-2004, 08:49 AM
I though I posted a previous solution to this. Maybe I deleted it. In any case:

in this script

function X() {
var yPos = window.event.clientY + document.body.scrollTop;
document.forms[0].val1.value=yPos;
return true; //add this
}


then in your <A> tag:


<a href="#" onclick="return ((X() && myMethod('test')) ? true:false)">

lisaliebe
02-11-2004, 09:56 AM
<a href="#" onclick="return ((X() && myMethod('test')) ? true:false)">

That worked ! Thanks a mill