Forum Moderators: open

Message Too Old, No Replies

using # in a hrefs

how to use onclick and # in hrefs

         

Ryshyn

9:37 pm on Feb 3, 2002 (gmt 0)



Can someone advise as im getting problems using the # in hrefs as in

<a href='#' onClick='java.function()'>

I see lots other sites use it working (runs the function) and not trying to run the #. but when I use it it always goes to

currentpage.html# - or the start page of the code, and looks for a anchor instead, & opens the page like a directory.

thsi is in fastbrowser and IE55, Have tried null and #null too, tries to find those current links, too. I need it to ignore the href totally, go to function.
thanks

txbakers

12:41 am on Feb 4, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



could be because you're calling java.function instead of javascript:function

you don't need the word javascript in the onClick

but your href syntax is correct.

joshie76

10:35 am on Feb 4, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are alternative ways of handling a javascript event in a href link:

1.
<a href="javscript:function()">link</a>
This is the most common as it avoids any problems with the #, however sometimes you don't want a JS function to fire from the href as you can't grab the 'event'. If you need to grab the event then you can use:

2.
<a href="javascript:;" onClick="function()">link</a>
The javascript:; is basically saying run nothing here. The onClick event doesn't require the javascript: as JS is expected in events. If you use the # instead of javascript:; browsers like IE will always jump to the top of the page.

Ryshyn

2:16 am on Feb 8, 2002 (gmt 0)



Thanks for the feedback; I dont have any java functions in the code im working on, but I will try your suggestion, joshie, i'venever seen that b4; I also think I found using a href='void(0)' might work, havent tried yet but I saw it cruising the web (I was cruising not it lol) thanks again.