...

View Full Version : Not to use <a href="javascript:..."



cdc08x
10-18-2005, 02:05 PM
some of the links I built were made so.
I read on this forum it's not a good practice and it's true, so I'm converting them all to the

<a href="..." onclick="foo()">...</a>
style.
The problem is that even if I made the onclick-functions return
false, the anchor redirects me anyway to the page that's in the href attribute. Is there a reason?
May someone help me?
Thanx

Brandoe85
10-18-2005, 06:19 PM
If you made the function your calling return false, then in your onclick handler call it like so:
<a href="" onclick="return foo();">link</a>


Good luck

jbezweb
10-18-2005, 06:22 PM
you could use href="#"

cdc08x
10-18-2005, 08:02 PM
For a strange reason, if I set it so:
<a href="..." onclick="foo();return false;">
the browser seem to work the right way...

Maybe once I'll understand :)
who knows...!!!
Thanx anyway!!

Brandoe85
10-18-2005, 08:07 PM
Can you explain, i'm not understanding? Are you putting return false in foo()? Post your code example and we'll be able to take a look at it.

felgall
10-18-2005, 10:49 PM
Method 1:


function foo() {
....
return false;
}

<a href="#" onclick="return foo()">

Method 2:


function foo() {
....
}

<a href="#" onclick="foo();return false">

Method 1 allows the function to return true if you want the href to be able to run (in which case you'd replace the # with a new page name).

cdc08x
10-18-2005, 10:52 PM
Thanx Stephen, now I understand!!
If I don't set a 'return' statement before the function-call, it's not expected the function returns anything!! Here's why!!!!
Thanx a lot!!!!

Brandoe85
10-18-2005, 11:46 PM
If you made the function your calling return false, then in your onclick handler call it like so:
<a href="" onclick="return foo();">link</a>


Exactly as I allready stated....

Kor
10-19-2005, 09:15 AM
The reson for, in that case, you must use return false is only to prevent the page's scrolling up. "#" location will bring the page up, so that you need a return false to prevent this HTML action.

cdc08x
10-19-2005, 03:29 PM
Thank you guys.
sorry Brando I didn't recognize the return statement immediately in your posted code :) thanx very much to you all!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum